Contents

swiftpackagerepository/gamekitservice.swift

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

GameKit (GameCenter) helper for Swift

GameKitService is created and maintaned with ❥ by Sascha Muellner.


[[Swift]](https://github.com/SwiftPackageRepository/GameKitService.swift/actions?query=workflow%3ASwift) [[codecov]](https://codecov.io/gh/SwiftPackageRepository/GameKitService.swift) [[Platform]](https://swiftpackageindex.com/SwiftPackageRepository/GameKitService.swift) [[License]](https://github.com/SwiftPackageRepository/GameKitService.swift/blob/main/LICENSE) [Version] [[Swift Version]](https://swiftpackageindex.com/SwiftPackageRepository/GameKitService.swift) [[SPM compatible]](https://github.com/apple/swift-package-manager) [[README]](https://SwiftPackageRepository.github.io/GameKitService.swift)

What?

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.

Requirements

The latest version of GameKitService requires:

  • Swift 5+
  • iOS 13+
  • Xcode 11+

Installation

Swift Package Manager

Using SPM add the following to your dependencies

How to use?

Starting a match

Given you already authenticated the user and did initiate a match you, using for example GCHelper or GameKitUI, you can now start it using start method from the GameKitService:

import GameKit
import GameKitService

let match: GKMatch

GameKitService
    .shared
    .start(match)

Subscribing to match data changes

The following match data changes can be subscribed using the GameKitService.

Authenticated

Subscribe to the authenticated: CurrentValueSubject<Bool, Never> CurrentValueSubject, to receive when the user is authenticated at the GameCenter.

import GameKit
import GameKitService

let match: GKMatch

GameKitService
    .shared
    .authenticated(match)
Match start

Subscribe to the started: PassthroughSubject<GKMatch, Never> PassthroughSubject, to receive data about the starting of the match.

var cancellable: AnyCancellable?

self.cancellable = GameKitService
    .ended
    .received.sink { (match: GKMatch) in
        // match: the ending match
    }
Match data

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.

var cancellable: AnyCancellable?

self.cancellable = GameKitService
    .shared
    .received.sink { (match: GKMatch, data: Data, player: GKPlayer) in
        // match: the current match
        // data: the data send from
        // player: the player that did send the data  
    }
Match ended

Subscribe to the ended: PassthroughSubject<GKMatch, Never> PassthroughSubject, to receive data about the ending of the match.

var cancellable: AnyCancellable?

self.cancellable = GameKitService
    .ended
    .received.sink { (match: GKMatch) in
        // match: the ending match
    }

Sending match data

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:

let data = "Hello Players!".data(using: .utf8)!

do {
    try GameKitService
        .shared
        .send(data)
} catch {
}

Where as the second possibility allows you to send to a dedicated group (one or more) of players in the match.

let playerOne: GKPlayer
let data = "Hello Player One!".data(using: .utf8)!

do {
    try GameKitService
        .shared
        .send(data, players: [playerOne])
} catch {
}

Documentation

Package Metadata

Repository: swiftpackagerepository/gamekitservice.swift

Default branch: main

README: README.md