Giter Site home page Giter Site logo

gamekitui.swift's Introduction

 

GameKitUI.swift

GameKit (GameCenter) helper for SwiftUI

GameKitUI is created and maintaned with ❥ by Sascha Muellner.


Swift codecov License Version SPM compatible README

What?

This is a Swift package with support for iOS that allows to use GameKit with SwiftUI.

Requirements

The latest version of GameKitUI requires:

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

Installation

Swift Package Manager

Using SPM add the following to your dependencies

'GameKitUI', 'main', 'https://github.com/SwiftPackageRepository/GameKitUI.swift.git'

How to use?

Views

GameCenter Authentication

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

import SwiftUI
import GameKitUI

struct ContentView: View {
	var body: some View {
		GKAuthenticationView { (state) in
			switch state {
			    case .started:
			    	print("Authentication Started")
			    	break
			    case .failed:
			    	print("Failed")
			    	break
			    case .deauthenticated:
					print("Deauthenticated")
			      	break
			    case .succeeded:
			    	break
			}
		} failed: { (error) in
			print("Failed: \(error.localizedDescription)")
		} authenticated: { (playerName) in
			print("Hello \(playerName)")
		}
	}
}

GameKit Invite

Invites created by a GameKit MatchMaker or TurnBasedMatchmaker can be handled using a GKMatchMakerView.

import SwiftUI
import GameKitUI

struct ContentView: View {
    var body: some View {
        GKInviteView(
            invite: GKInvite()
        ) {
        } failed: { (error) in
            print("Invitation Failed: \(error.localizedDescription)")
        } started: { (match) in
            print("Match Started")
        }
    }
}

GameKit MatchMaker

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

import SwiftUI
import GameKitUI

struct ContentView: View {
	var body: some View {
		GKMatchMakerView(
                    minPlayers: 2,
                    maxPlayers: 4,
                    inviteMessage: "Let us play together!"
                ) {
                    print("Player Canceled")
                } failed: { (error) in
                    print("Match Making Failed: \(error.localizedDescription)")
                } started: { (match) in
                    print("Match Started")
                }
	}
}

GameKit TurnBasedMatchmaker

To start a turn based match use GKTurnBasedMatchmakerView.

import SwiftUI
import GameKitUI

struct ContentView: View {
	var body: some View {
		GKTurnBasedMatchmakerView(
                    minPlayers: 2,
                    maxPlayers: 4,
                    inviteMessage: "Let us play together!"
                ) {
                    print("Player Canceled")
                } failed: { (error) in
                    print("Match Making Failed: \(error.localizedDescription)")
                } started: { (match) in
                    print("Match Started")
                }
	}
}

GameKit Manager

GKMatchManager

GameKitUI views rely on a manager singelton GKMatchManager, which listens to GameKit state changes of the match making process. Changes to the local player GKLocalPlayer, invites GKInvite or matches GKMatch can be observed using the provided public subjects CurrentValueSubject.

import SwiftUI
import GameKitUI

class ViewModel: ObservableObject {

    @Published public var gkInvite: GKInvite?
    @Published public var gkMatch: GKMatch?

    private var cancellableInvite: AnyCancellable?
    private var cancellableMatch: AnyCancellable?
    private var cancellableLocalPlayer: AnyCancellable?

    public init() {
        self.cancellableInvite = GKMatchManager
            .shared
            .invite
            .sink { (invite) in
                self.gkInvite = invite.gkInvite
        }
        self.cancellableMatch = GKMatchManager
            .shared
            .match
            .sink { (match) in
                self.gkMatch = match.gkMatch
        }
        self.cancellableLocalPlayer = GKMatchManager
            .shared
            .localPlayer
            .sink { (localPlayer) in
                // current GKLocalPlayer.local
        }
    }
    
    deinit() {
        self.cancellableInvite?.cancel()
        self.cancellableMatch?.cancel()
        self.cancellableLocalPlayer?.cancel()
    }
}

Examples

GKMatchMaker Example

The provided GKMatchMaker example, includes a full working SwiftUI solution for handling GameKit matchmaking. 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. The Config.xcconfig should now look something like this:

// Configuration settings file format documentation can be found at:
// https://help.apple.com/xcode/#/dev745c5c974

XCCONFIG_DEVELOPMENT_TEAM = 9988XX7D42 // YOUR DEVELOPMENT TEAM ID
XCCONFIG_BUNDLE_ID = com.yourcompany.ProductName // A BUNDLE ID WITH SUPPORT FOR THE GAMECENTER CAPABILITY

Then open the GKMatchMaker.xcodeproj and run it on as many real hardware devices to test the GameKit match making.

Documentation

gamekitui.swift's People

Contributors

gomfucius avatar pawello2222 avatar smuellner avatar thisisthefoxe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gamekitui.swift's Issues

Extra comments don't match Licence

Hi @smuellner

Thanks for developing this great package!I was hoping to use your GameKitUI in my app and looking at the Licence it seems that I can, but there are some comments in the code that possibly contradict the MIT Licence. Would it be possible to remove the contradictory statements or possibly clarify what unauthorised usage is?

Thanks

/// Unauthorized copying or usage of this file, via any medium is strictly prohibited.

How to accept invitations?

Hi, thanks for this library. I'm attempting to accept an invitation from another player and having trouble figuring out how to do that. The other player successfully gets the push notification, taps it, and the app launches, but then nothing happens.

I'm sure I need to do something in the app to see the invitation and accept it, but I'm not sure what that is. Any help would be appreciated. Thanks!

Show leaderboard

Is it possible to show the leaderboard aka the general GKGameCenterViewController using SwiftUI?

Documentation appears out of date

Following the example usage, GKAuthenticationView has three closures: state, failed, and authenticated. However, putting this example code into a SwiftUI view results in an error "Extra trailing closure passed in call" on the "failed:" line. Using

GKAuthenticationView(failed: { error in ...}, authenticated: { player in ...}

appears to work just fine.

How to load package without Examples?

Hi Sascha

Thank you for your excellent open source package. It will save me a lot of time.

I have downloaded and run your package using the Example app that was included and it works well.

I am now constructing my own app and want to include your GamKitUI library without all the extraneous source code for the Example app and platforms I don't plan to support. I am not too familiar with Swift Packages and was wanting to know if there is a way to add a stripped down version of your package that just satisfies the "import GameKitUI" requirements?

Many thanks

Robert

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.