GameKitService is created and maintaned with ❥ by Sascha Muellner.
What?
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.
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)
Subscribing to match data changes
The following match data changes can be subscribed using the GameKitService.
Authenticated
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)
Match start
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
}
Match data
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
}
Match ended
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
}
Sending match data
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 {
}
GameKitService.swift
GameKit (GameCenter) helper for Swift
GameKitService is created and maintaned with ❥ by Sascha Muellner.
What?
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.
Requirements
The latest version of GameKitService requires:
Installation
Swift Package Manager
Using SPM add the following to your dependencies
'GameKitService', 'master', 'https://github.com/SwiftPackageRepository/GameKitService.swift.git'
How to use?
Starting a match
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:
Subscribing to match data changes
The following match data changes can be subscribed using the GameKitService.
Authenticated
Subscribe to the
authenticated: CurrentValueSubject<Bool, Never>
CurrentValueSubject, to receive when the user is authenticated at the GameCenter.Match start
Subscribe to the
started: PassthroughSubject<GKMatch, Never>
PassthroughSubject, to receive data about the starting of the match.Match data
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.Match ended
Subscribe to the
ended: PassthroughSubject<GKMatch, Never>
PassthroughSubject, to receive data about the ending of the match.Sending match data
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:
Where as the second possibility allows you to send to a dedicated group (one or more) of players in the match.
Documentation