You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a **Swift** package with support for iOS/macOS/tvOS that allows to use GameKit.
18
+
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.
19
19
20
20
## Requirements
21
21
@@ -30,18 +30,119 @@ The latest version of GameKitService requires:
Given you already authenticated the user and did initiate a match you, using for example [GCHelper](https://github.com/jackcook/GCHelper.git) or [GameKitUI](https://github.com/SwiftPackageRepository/GameKitUI.swift.git), you can now start it using **start** method from the **GameKitService**:
40
40
41
41
```swift
42
+
importGameKit
43
+
importGameKitService
42
44
45
+
let match: GKMatch
46
+
47
+
GameKitService
48
+
.shared
49
+
.start(match)
50
+
```
51
+
52
+
### Subscribing to match data changes
53
+
54
+
The following match data changes can be subscribed using the **GameKitService**.
55
+
56
+
#### Authenticated
57
+
58
+
Subscribe to the `authenticated: CurrentValueSubject<Bool, Never>` CurrentValueSubject, to receive when the user is authenticated at the GameCenter.
59
+
60
+
```swift
61
+
importGameKit
62
+
importGameKitService
63
+
64
+
let match: GKMatch
65
+
66
+
GameKitService
67
+
.shared
68
+
.authenticated(match)
69
+
```
70
+
71
+
#### Match start
72
+
73
+
Subscribe to the `started: PassthroughSubject<GKMatch, Never>` PassthroughSubject, to receive data about the starting of the match.
74
+
75
+
```swift
76
+
var cancellable: AnyCancellable?
77
+
78
+
self.cancellable= GameKitService
79
+
.ended
80
+
.received.sink { (match: GKMatch) in
81
+
// match: the ending match
82
+
}
83
+
```
84
+
85
+
#### Match data
86
+
87
+
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.
88
+
89
+
```swift
90
+
var cancellable: AnyCancellable?
91
+
92
+
self.cancellable= GameKitService
93
+
.shared
94
+
.received.sink { (match: GKMatch, data: Data, player: GKPlayer) in
95
+
// match: the current match
96
+
// data: the data send from
97
+
// player: the player that did send the data
98
+
}
99
+
```
100
+
101
+
#### Match ended
102
+
103
+
Subscribe to the `ended: PassthroughSubject<GKMatch, Never>` PassthroughSubject, to receive data about the ending of the match.
104
+
105
+
```swift
106
+
var cancellable: AnyCancellable?
107
+
108
+
self.cancellable= GameKitService
109
+
.ended
110
+
.received.sink { (match: GKMatch) in
111
+
// match: the ending match
112
+
}
43
113
```
44
114
115
+
### Sending match data
116
+
117
+
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:
118
+
119
+
```swift
120
+
let data ="Hello Players!".data(using: .utf8)!
121
+
122
+
do {
123
+
try GameKitService
124
+
.shared
125
+
.send(data)
126
+
} catch {
127
+
}
128
+
```
129
+
130
+
Where as the second possibility allows you to send to a dedicated group (one or more) of players in the match.
+[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