Swiftpack.co - fserrazes/GameCenterKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by fserrazes.
fserrazes/GameCenterKit 0.6.0
Game Center package wrapper for UIKIt and SwiftUI
⭐️ 7
🕓 6 weeks ago
.package(url: "https://github.com/fserrazes/GameCenterKit.git", from: "0.6.0")


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+


Swift Package Manager

Using SPM add the following to your dependencies

'GameCenterKit', 'main', 'https://github.com/fserrazes/GameCenterKit.git'

How to use?


1. The local player must be authenticated on Game Center
2. Your app need an identifier leaderboard defined in App Store Connect.

First the local player must be authenticated on Game Center.

Authenticates the local player with in Game Center if it's possible.

do {
    let isAuthenticated = try await GameCenterKit.shared.authenticate()
    if isAuthenticated {
        // Local player is authenticated
    } else {
        // Local player is not authenticated
} catch {
    // Handle any errors that might occur during authentication

To presents the Game Center view provided by GameKit there are 3 options:

1. Toggle AccessPointView
2. Open from a ViewController (UIKit)
3. Open from a View (SwiftUI)

Toggle AccessPointView


Open from a ViewController (UIKit)

do {
    try GameCenterKit.shared.showGameCenter(viewController: self)
} catch {

Open from a View (SwiftUI)

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)
        .sheet(isPresented: $isGameCenterOpen) {

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

Leaderboards actions

let identifierId: String = "your-app-leaderboard-id"

Retrieve Score

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

Retrieve Rank

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 {

Retrieve Best Players

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 {

Submit Score

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 {

Achievements actions

let achievementId: String = "your-app-achievement-id"

Submit Achievement

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 {

Reset Achievements

Resets the percentage completed for all of the player’s achievements.

do {
    try await GameCenterKit.shared.resetAchievements()
} catch {



Stars: 7
Last commit: 6 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics