https://github.com/v-braun/gcconnection
GameCenter multiplayer connection util
https://github.com/v-braun/gcconnection
gamecenter gamekit gkmatchmaker ios matchmaking multiplayer-game swift
Last synced: 8 months ago
JSON representation
GameCenter multiplayer connection util
- Host: GitHub
- URL: https://github.com/v-braun/gcconnection
- Owner: v-braun
- License: mit
- Created: 2019-01-02T09:21:17.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-26T07:24:47.000Z (almost 7 years ago)
- Last Synced: 2025-04-12T22:43:35.215Z (about 1 year ago)
- Topics: gamecenter, gamekit, gkmatchmaker, ios, matchmaking, multiplayer-game, swift
- Language: Swift
- Size: 484 KB
- Stars: 4
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# GCConnection
> GameCenter multiplayer connection util
By [v-braun - viktor-braun.de](https://viktor-braun.de).
[](https://github.com/v-braun/GCConnection/blob/master/LICENSE)
[](https://travis-ci.org/v-braun/GCConnection)

## Description
## Installation
1. Download and drop ```GCConnection.swift``` in your project.
2. Congratulations!
## Usage
For detailed usage check out the demo [ViewController.swift](https://github.com/v-braun/GCConnection/blob/master/GCConnection/ViewController.swift) file.
### Authenticate
In your AppDelegate invoke *authenticate*
``` swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GCConnection.shared.authenticate()
return true
}
```
Later on in your ViewControlelr you can check the authenticate status
``` swift
switch GCConnection.shared.authStatus {
case .undef:
// not authenticated
case .loginCancelled:
// login canccelled 🙅♀️
case .error(let err):
// auth err
case .loginRequired(let viewController):
// login required
// show present ciewController - it is the GC login view
case .ok(let localPlayer):
// authenticated 🥳
}
```
You can also listen to authentication state changes!
Implement the *AuthHandler* protocol and set the authHandler property
``` swift
override func viewDidLoad() {
super.viewDidLoad()
GCConnection.shared.authHandler = self
}
extension ViewController : AuthHandler{
func handle(connection: GCConnection, authStatusChanged: AuthStatus) {
// your code ...
}
}
```
### Match making
To create a match simply call *findMatch*.
To listen on status updates you should set the *handler* property on the match.
``` swift
let match = try! GCConnection.shared.findMatch(minPlayers: 2, maxPlayers: 2)
match.handler = self
```
Example implementation of the *MatchHandler* protocol:
``` swift
extension ViewController : MatchHandler{
func handle(_ error: Error) {
// error
}
func handle(_ state: MatchState) {
// status update
}
func handle(data: Data, fromPlayer: GKPlayer) {
// received data from given player
}
func handle(playerDisconnected: GKPlayer) {
// given player has disconnected
}
}
```
You can always cancel a match with
``` swift
match.cancel()
```
The active match is always accessible on the shared GCConnection instance
``` swift
GCConnection.shared.activeMatch
```
### Send data
To send data use broadCast method
``` swift
let data = "hello".data(using: .utf8)
try! GCConnection.shared.activeMatch!.broadCast(data: data!, withMode: GKMatch.SendDataMode.reliable)
```
## Configuration
### Prepare App for GameCenter support
First you should enable the GameCenter feature in your ap.
Go to Project/Capabilities and enable GameCenter

After that you have to register your app in [App Store Connect](https://appstoreconnect.apple.com/).
Login with your account and go to **My Apps**

On the Dashboard add a new App

Enter the needed information.
**IMPORTANT:** The BundleIdentifier should match your Project setting
During my tests I found out that GameCenter will not recognize your app until you create at least one leaderboard.
Goto /Features/Game Center/Leaderboard

## Related Projects
[Cocoa Rocks](https://cocoa.rocks/): a gallery of beatiful Cocoa Controls
[awesome-cocoa](https://github.com/v-braun/awesome-cocoa): an awesome list of cocoa controls
## Authors

[v-braun](https://github.com/v-braun/)
## Contributing
Make sure to read these guides before getting started:
- [Contribution Guidelines](https://github.com/v-braun/GCConnection/blob/master/CONTRIBUTING.md)
- [Code of Conduct](https://github.com/v-braun/GCConnection/blob/master/CODE_OF_CONDUCT.md)
## License
**GCConnection** is available under the MIT License. See [LICENSE](https://github.com/v-braun/GCConnection/blob/master/LICENSE) for details.