2020 Graphics & Games
WWDC20 · 25 min · Graphics & Games
Tap into Game Center: Leaderboards, Achievements, and Multiplayer
Level up your Game Center integration and enable players to compare scores on leaderboards, earn valuable achievements, and engage with other players. Organize special events like weekly championships, daily showdowns, or 1-hour competitions using recurring leaderboards. Create up to 100 unique achievements for your game. And we’ll show you how to set up real-time or turn-based multiplayer matches for your Game Center players. If you want to learn more about Game Center’s interface, Dashboard, and player profiles, check out “Tap into Game Center: Dashboard, Access Point, and Profile.” And for more about preparing your game’s interface for these new capabilities, see “Design for Game Center.”
Watch at developer.apple.com ↗Code shown on screen · 8 snippets
Submitting a score
// Use the class method to submit score to one or more leaderboards at once
GKLeaderboard.submitScore(self.points, context: 0, player: GKLocalPlayer.local,
leaderboardIDs: ["my.leaderboard.id"]) { error in
} Submitting a score - recurring leaderboard ID
// Use the class method to submit score to one or more leaderboards at once
GKLeaderboard.submitScore(self.points, context: 0, player: GKLocalPlayer.local,
leaderboardIDs: ["my.recurring.leaderboard.id"]) { error in
} Submitting to a specific occurrence of a recurring leaderboard
// Submitting to a specific occurrence of a recurring leaderboard
GKLeaderboard.loadLeaderboards(IDs:["my.recurring.leaderboard.id"]) { (fetchedLBs, error) in
if let lb = fetchedLBs?.first {
lb.submitScore(self.points, context: 0, player: GKLocalPlayer.local) { error in
}
}
} Launching in-game UI
// Launching in-game UI
// Display a list of leaderboards
let vc = GKGameCenterViewController(
state: .leaderboards)
vc.gameCenterDelegate = self
present(vc, animated: true, completion: nil)
// Or directly display scores for a specific leaderboard
let vc = GKGameCenterViewController(
leaderboardID: "YOUR_ASC_LEADERBOARD_ID",
playerScope: .global,
timeScope: .allTime)
vc.gameCenterDelegate = self
present(vc, animated: true, completion: nil) Accessing previous occurrence
// Accessing previous occurrence
// Load current occurrence of a recurring leaderboard
GKLeaderboard.loadLeaderboards(IDs:["my.recurring.leaderboard.id"]) { (fetchedLBs, error) in
if let current = fetchedLBs?.first {
// Load previous occurrence using the current occurrence
current.loadPreviousOccurrence { (prevOccurrence, error) in
// Do something with the previous occurrence
}
}
} Reporting achievement progress
if let achievement = GKAchievement(identifier: identifier) {
achievement.percentComplete = percentComplete
GKAchievement.report([achievement]) { error in
if let error = error {
print("Error in reporting achievements: \(error)")
}
}
} Displaying Game Center achievements
// Showing the Game Center achievements page
let viewController = GKGameCenterViewController(state: .achievements)
viewController.gameCenterDelegate = self
present(viewController, animated: true) Check if personalized communication is restricted
// Check if personalized communication is restricted
if GKLocalPlayer.local.personalizedCommunicationRestricted {
// Disable UI for Voice chat
} Related sessions
-
10 min -
27 min -
22 min -
26 min -
18 min -
24 min