GameKitUI.swift
GameKit (GameCenter) helper for SwiftUI
GameKitUI is created and maintaned with ❥ by Sascha Muellner.
What?
This is a Swift package with support for iOS/macOS/tvOS that allows to use GameKit with SwiftUI.
Requirements
The latest version of GameKitUI requires:
- Swift 5+
- iOS 13+
- Xcode 11+
Installation
Swift Package Manager
Using SPM add the following to your dependencies
'GameKitUI', 'master', 'https://github.com/smuellner/GameKitUI.swift.git'
How to use?
GameCenter Authentication
To authenticate the player with GameCenter just show the authentication view GKAuthenticationView.
import SwiftUI
import GameKitUI
struct ContentView: View {
var body: some View {
GKAuthenticationView { (state) in
switch state {
case .started:
print("Authentication Started")
break
case .failed:
print("Failed")
break
case .deauthenticated:
print("Deauthenticated")
break
case .succeeded:
break
}
} failed: { (error) in
print("Failed: \(error.localizedDescription)")
} authenticated: { (playerName) in
print("Hello \(playerName)")
}
}
}
GameKit MatchMaker
Match making for a live match can be initiated via the GKMatchMakerView.
import SwiftUI
import GameKitUI
struct ContentView: View {
var body: some View {
GKMatchMakerView(
minPlayers: 2,
maxPlayers: 4,
inviteMessage: "Let us play together!"
) {
print("Player Canceled")
} failed: { (error) in
print("Match Making Failed: \(error.localizedDescription)")
} started: { (match) in
print("Match Started")
}
}
}
GameKit TurnBasedMatchmaker
To start a turn based match use GKTurnBasedMatchmakerView.
import SwiftUI
import GameKitUI
struct ContentView: View {
var body: some View {
GKTurnBasedMatchmakerView(
minPlayers: 2,
maxPlayers: 4,
inviteMessage: "Let us play together!"
) {
print("Player Canceled")
} failed: { (error) in
print("Match Making Failed: \(error.localizedDescription)")
} started: { (match) in
print("Match Started")
}
}
}