Skip to content

Commit 4e322f7

Browse files
committed
Updated Documentation and licenses
1 parent 9b28c64 commit 4e322f7

File tree

3 files changed

+151
-11
lines changed

3 files changed

+151
-11
lines changed

README.md

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GameKitService.swift
22

3-
## GameKit (GameCenter) helper for SwiftUI
3+
## GameKit (GameCenter) helper for Swift
44

55
GameKitService is **created and maintaned with ❥** by Sascha Muellner.
66

@@ -15,7 +15,7 @@ GameKitService is **created and maintaned with ❥** by Sascha Muellner.
1515
[![README](https://img.shields.io/badge/-README-lightgrey)](https://SwiftPackageRepository.github.io/GameKitService.swift)
1616

1717
## What?
18-
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.
1919

2020
## Requirements
2121

@@ -30,18 +30,119 @@ The latest version of GameKitService requires:
3030
### Swift Package Manager
3131
Using SPM add the following to your dependencies
3232

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

3535
## How to use?
3636

37-
### GameCenter
37+
### Starting a match
3838

39-
To au.
39+
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**:
4040

4141
```swift
42+
import GameKit
43+
import GameKitService
4244

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+
import GameKit
62+
import GameKitService
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+
}
43113
```
44114

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.
131+
132+
```swift
133+
let playerOne: GKPlayer
134+
let data = "Hello Player One!".data(using: .utf8)!
135+
136+
do {
137+
try GameKitService
138+
.shared
139+
.send(data, players: [playerOne])
140+
} catch {
141+
}
142+
```
143+
144+
145+
45146
## Documentation
46147
+ [Apple Documentation GameKit](https://developer.apple.com/documentation/gamekit/)
47148
+ [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)

Sources/GameKitService/GameKitService.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
///
2-
/// GameKitService.swift
2+
/// MIT License
33
///
4+
/// Copyright (c) 2020 Sascha Müllner
5+
///
6+
/// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
/// of this software and associated documentation files (the "Software"), to deal
8+
/// in the Software without restriction, including without limitation the rights
9+
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
/// copies of the Software, and to permit persons to whom the Software is
11+
/// furnished to do so, subject to the following conditions:
12+
///
13+
/// The above copyright notice and this permission notice shall be included in all
14+
/// copies or substantial portions of the Software.
15+
///
16+
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
/// SOFTWARE.
423
///
524
/// Created by Sascha Müllner on 04.12.20.
625

@@ -30,6 +49,8 @@ public class GameKitService : NSObject, GKMatchDelegate, GKLocalPlayerListener,
3049
selector: #selector(GameKitService.authenticationChanged),
3150
name: Notification.Name.GKPlayerAuthenticationDidChangeNotificationName,
3251
object: nil)
52+
53+
GKLocalPlayer.local.register(self)
3354
}
3455

3556
// MARK: Public functions
@@ -43,7 +64,7 @@ public class GameKitService : NSObject, GKMatchDelegate, GKLocalPlayerListener,
4364
guard let match = self.match else { return }
4465
try match.sendData(toAllPlayers: data, with: .reliable)
4566
}
46-
67+
4768
public func send(_ data: Data, players: [GKPlayer]) throws {
4869
guard let match = self.match else { return }
4970
try match.send(data, to: players, dataMode: .reliable)
@@ -84,7 +105,6 @@ public class GameKitService : NSObject, GKMatchDelegate, GKLocalPlayerListener,
84105

85106
// MARK: GKLocalPlayerListener
86107
public func player(_ player: GKPlayer, didAccept invite: GKInvite) {
87-
88108
}
89109

90110
// MARK: Private functions

Sources/GameKitService/GameKitServiceProtocol.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
///
2-
/// GameKitServiceProtocol.swift
3-
///
4-
///
2+
/// MIT License
3+
///
4+
/// Copyright (c) 2020 Sascha Müllner
5+
///
6+
/// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
/// of this software and associated documentation files (the "Software"), to deal
8+
/// in the Software without restriction, including without limitation the rights
9+
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
/// copies of the Software, and to permit persons to whom the Software is
11+
/// furnished to do so, subject to the following conditions:
12+
///
13+
/// The above copyright notice and this permission notice shall be included in all
14+
/// copies or substantial portions of the Software.
15+
///
16+
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
/// SOFTWARE.
23+
///
524
/// Created by Sascha Müllner on 04.12.20.
625

726
import Combine

0 commit comments

Comments
 (0)