This is a Swift package with support for iOS that allows to use GameKit with UIKit and SwiftUI.
Enable players to interact with friends, compare leaderboard ranks and earn achievements.
The latest version of GameKitUI requires:
- Swift 5+
- iOS 14+
- Xcode 13+
Using SPM add the following to your dependencies
'GameCenterKit', 'main', 'https://github.com/fserrazes/GameCenterKit.git'
1. The local player must be authenticated on Game Center
2. Your app need an identifier leaderboard defined in App Store Connect.
Authenticates the local player with in Game Center if it's possible.
do {
GameCenterKit.shared.authenticate { isAuthenticated in
if isAuthenticated {
// Local player is authenticated
} else {
// Local player is not authenticated
}
} catch {
// Handle any errors that might occur during authentication
}
1. Toggle AccessPointView
2. Open from a ViewController (UIKit)
3. Open from a View (SwiftUI)
GameCenterKit.shared.toogleGameAccessPointView()
do {
try GameCenterKit.shared.showGameCenter(viewController: self)
} catch {
print(error)
}
import SwiftUI
import GameCenterKit
struct ContentView: View {
@State var isGameCenterOpen: Bool = false
var body: some View {
VStack {
Button {
isGameCenterOpen = true
} label: {
Text("Open GameCenter View")
.padding(.all, 5.0)
}
.buttonStyle(.borderedProminent)
}
.sheet(isPresented: $isGameCenterOpen) {
GameCenterView()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
let identifierId: String = "your-app-leaderboard-id"
The score earned by the local player (time scope defined is all time).
let bestScore: Int = 0
if let score = try await GameCenterKit.shared.retrieveScore(identifier: identifierId) {
print("best score: \(String(describing: score))")
self.bestScore = score
}
The rank earned by the local player (time scope defined is all time).
do {
let (current, previous) = try await GameCenterKit.shared.retrieveRank(identifier: identifierId)
print("current rank: \(String(describing: current))")
print("previous rank: \(String(describing: previous))")
} catch {
print(error)
}
The best players list and the number of total players (time scope defined is all time).
// Number of top players (1 - 50) to use for getting the scores.
let topPlayers: Int = 10
do {
let (players, totalPlayers) = try await GameCenterKit.shared.retrieveBestPlayers(identifier: identifierId, topPlayers: topPlayers)
print("total players: \(String(describing: totalPlayers))")
for player in players {
print("player: \(player.displayName)\t score: \(player.leaderboard.score)")
}
} catch {
print(error)
}
Report a high score eligible for placement on a leaderboard.
// The score earned by the local player
let score: Int = 10
do {
try await GameCenterKit.shared.submitScore(score: score, identifier: identifierId)
} catch {
print(error)
}
let achievementId: String = "your-app-achievement-id"
Reports progress on an achievement, if it has not been completed already.
// A percentage value (0 - 100) stating how far the user has progressed on the achievement
let percent: Double = 10.0
do {
try await GameCenterKit.shared.submitAchievement(identifier: achievementId, percent: percent)
} catch {
print(error)
}
Resets the percentage completed for all of the player’s achievements.
do {
try await GameCenterKit.shared.resetAchievements()
} catch {
print(error)
}