Skip to content

Commit 8537e34

Browse files
committed
Improved documentation
1 parent b4a8d02 commit 8537e34

1 file changed

Lines changed: 98 additions & 5 deletions

File tree

README.md

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ The latest version of GameKitUI requires:
3030
### Swift Package Manager
3131
Using SPM add the following to your dependencies
3232

33-
``` 'GameKitUI', 'master', 'https://github.com/smuellner/GameKitUI.swift.git' ```
33+
``` 'GameKitUI', 'main', 'https://github.com/smuellner/GameKitUI.swift.git' ```
3434

3535
## How to use?
3636

37-
### GameCenter Authentication
37+
38+
### Views
39+
40+
#### GameCenter Authentication
3841

3942
To authenticate the player with GameCenter just show the authentication view **GKAuthenticationView**.
4043

@@ -67,7 +70,29 @@ struct ContentView: View {
6770
}
6871
```
6972

70-
### GameKit MatchMaker
73+
#### GameKit Invite
74+
75+
Invites created by a GameKit MatchMaker or TurnBasedMatchmaker can be handled using a **GKMatchMakerView**.
76+
77+
```swift
78+
import SwiftUI
79+
import GameKitUI
80+
81+
struct ContentView: View {
82+
var body: some View {
83+
GKInviteView(
84+
invite: GKInvite()
85+
) {
86+
} failed: { (error) in
87+
print("Invitation Failed: \(error.localizedDescription)")
88+
} started: { (match) in
89+
print("Match Started")
90+
}
91+
}
92+
}
93+
```
94+
95+
#### GameKit MatchMaker
7196

7297
Match making for a live match can be initiated via the **GKMatchMakerView**.
7398

@@ -92,8 +117,7 @@ struct ContentView: View {
92117
}
93118
```
94119

95-
96-
### GameKit TurnBasedMatchmaker
120+
#### GameKit TurnBasedMatchmaker
97121

98122
To start a turn based match use **GKTurnBasedMatchmakerView**.
99123

@@ -118,6 +142,75 @@ struct ContentView: View {
118142
}
119143
```
120144

145+
### GameKit Manager
146+
147+
#### GKMatchManager
148+
149+
GameKitUI views rely on a manager singelton **GKMatchManager**, which listens to GameKit state changes of the match making process.
150+
Changes to the local player **GKLocalPlayer**, invites **GKInvite** or matches **GKMatch** can be observed using the provided public subjects **CurrentValueSubject**.
151+
152+
```swift
153+
import SwiftUI
154+
import GameKitUI
155+
156+
class ViewModel: ObservableObject {
157+
158+
@Published public var gkInvite: GKInvite?
159+
@Published public var gkMatch: GKMatch?
160+
161+
private var cancellableInvite: AnyCancellable?
162+
private var cancellableMatch: AnyCancellable?
163+
private var cancellableLocalPlayer: AnyCancellable?
164+
165+
public init() {
166+
self.cancellableInvite = GKMatchManager
167+
.shared
168+
.invite
169+
.sink { (invite) in
170+
self.gkInvite = invite.gkInvite
171+
}
172+
self.cancellableMatch = GKMatchManager
173+
.shared
174+
.match
175+
.sink { (match) in
176+
self.gkMatch = match.gkMatch
177+
}
178+
self.cancellableLocalPlayer = GKMatchManager
179+
.shared
180+
.localPlayer
181+
.sink { (localPlayer) in
182+
// current GKLocalPlayer.local
183+
}
184+
}
185+
186+
deinit() {
187+
self.cancellableInvite?.cancel()
188+
self.cancellableMatch?.cancel()
189+
self.cancellableLocalPlayer?.cancel()
190+
}
191+
}
192+
```
193+
194+
### Examples
195+
196+
#### GKMatchMaker Example
197+
198+
The provided **GKMatchMaker** example, includes a full working SwiftUI solution for handling GameKit matchmaking.
199+
Just copy the file **Config.xcconfig-example** to **Config.xcconfig** and add your development team ID for the variable **XCCONFIG_DEVELOPMENT_TEAM** and a valid Bundle ID with GameCenter support for **XCCONFIG_BUNDLE_ID**.
200+
The **Config.xcconfig** should now look something like this:
201+
202+
```config
203+
// Configuration settings file format documentation can be found at:
204+
// https://help.apple.com/xcode/#/dev745c5c974
205+
206+
XCCONFIG_DEVELOPMENT_TEAM = 9988XX7D42 // YOUR DEVELOPMENT TEAM ID
207+
XCCONFIG_BUNDLE_ID = domain.host.application // A BUNDLE ID WITH SUPPORT FOR THE GAMECENTER CAPABILITY e.g. domain.host.application
208+
```
209+
210+
211+
Then open the **GKMatchMaker.xcodeproj** and run it on as many **real hardware** devices to test the GameKit match making.
212+
213+
121214
## Documentation
122215
+ [Apple Documentation GameKit](https://developer.apple.com/documentation/gamekit/)
123216
+ [raywenderlich.com: Game Center for iOS: Building a Turn-Based Game](https://www.raywenderlich.com/7544-game-center-for-ios-building-a-turn-based-game)

0 commit comments

Comments
 (0)