GameKitService is created and maintaned with ❥ by Sascha Muellner.
This is a Swift package with support for iOS/macOS/tvOS that focuses on bridging the current GameKit implementation to a single service structure utilizing Combine to simplify and modernize GameKit's match handling.
The latest version of GameKitService requires:
- Swift 5+
- iOS 13+
- Xcode 11+
Using SPM add the following to your dependencies
'GameKitService', 'master', 'https://github.com/SwiftPackageRepository/GameKitService.swift.git'
Given you already authenticated the user and did initiate a match you, using for example GCHelper or GameKitUI, you can now start it using start method from the GameKitService:
import GameKit
import GameKitService
let match: GKMatch
GameKitService
.shared
.start(match)
The following match data changes can be subscribed using the GameKitService.
Subscribe to the authenticated: CurrentValueSubject<Bool, Never>
CurrentValueSubject, to receive when the user is authenticated at the GameCenter.
import GameKit
import GameKitService
let match: GKMatch
GameKitService
.shared
.authenticated(match)
Subscribe to the started: PassthroughSubject<GKMatch, Never>
PassthroughSubject, to receive data about the starting of the match.
var cancellable: AnyCancellable?
self.cancellable = GameKitService
.ended
.received.sink { (match: GKMatch) in
// match: the ending match
}
Subscribe to the received: PassthroughSubject<(match: GKMatch, data: Data, player: GKPlayer), Never>
PassthroughSubject, to receive data about the match from another player's device in the match.
var cancellable: AnyCancellable?
self.cancellable = GameKitService
.shared
.received.sink { (match: GKMatch, data: Data, player: GKPlayer) in
// match: the current match
// data: the data send from
// player: the player that did send the data
}
Subscribe to the ended: PassthroughSubject<GKMatch, Never>
PassthroughSubject, to receive data about the ending of the match.
var cancellable: AnyCancellable?
self.cancellable = GameKitService
.ended
.received.sink { (match: GKMatch) in
// match: the ending match
}
To send data to other players in the match there are two possibilites. In the first one the data is send to all players in the match:
let data = "Hello Players!".data(using: .utf8)!
do {
try GameKitService
.shared
.send(data)
} catch {
}
Where as the second possibility allows you to send to a dedicated group (one or more) of players in the match.
let playerOne: GKPlayer
let data = "Hello Player One!".data(using: .utf8)!
do {
try GameKitService
.shared
.send(data, players: [playerOne])
} catch {
}