GameCenterUI
The easiest way to integrate GameCenter with SwiftUI.
Description
GameCenterUI enables use of Apple's GameKit framework from SwiftUI. Both iOS and Mac are supported.
Demo
Usage
Call .enableGameCenter()
inside the App
struct to authenticate with GameCenter.
import SwiftUI
import GameCenterUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.enableGameCenter()
}
}
}
A view showing the highscore in a button that presets the leaderboard if authenticated. When not authenticated the button will take the user to the preferences where one can log in to GameCenter.
struct MyGameView: View {
@Binding var highscore: Int
@Environment(\.gameCenterIsAuthenticated) var gameCenterIsAuthenticated
@State var showGameCenter = false
var body: some View {
if gameCenterIsAuthenticated {
Button("Highscore \(highscore)") {
showGameCenter.toggle()
}
.gameCenter(
isPresented: $showGameCenter,
launchOption: .leaderBoardID(
id: "SomeLeaderboardID",
playerScope: .global,
timeScope: .allTime))
} else {
Button("Log in to game center") {
openGameCenterConfiguration()
}
}
}
}
Installation
Package.swift
Edit the Package.swift file. Add the GamCenterUI as a dependency:
let package = Package(
name: " ... ",
products: [ ... ],
dependencies: [
.package(url: "https://github.com/berikv/GameCenterUI.git", from: "1.0.0") // here
],
targets: [
.target(
name: " ... ",
dependencies: [
"GameCenterUI" // and here
]),
]
)
For .xcodeproj projects
- Open menu File > Add Packages...
- Search for "https://github.com/berikv/GameCenterUI.git" and click Add Package.
- Open your project file, select your target in "Targets".
- Open Dependencies
- Click the + sign
- Add GameCenterUI