2021 App ServicesApp Store, Distribution & MarketingEssentialsGraphics & Games
WWDC21 · 22 min · App Services / App Store, Distribution & Marketing / Essentials / Graphics & Games
What’s new in Game Center: Widgets, friends, and multiplayer improvements
Power up your online gaming experience with GameKit and adopt features like multiplayer, leaderboards, and achievements in your game. We’ll take you through the latest improvements to Game Center, including player matching and multiplayer APIs, and explore how you can boost discovery of your game.
Watch at developer.apple.com ↗Code shown on screen · 6 snippets
Friending API
// Call Friend Requests API to present friend request view from a view controller, when player click on Add Friends Button in your game
let error = GKLocalPlayer.local
.presentFriendRequestCreatorFromViewController(using: navigationController)
if error != nil {
print("Fail to send friend request with error: \(error!.localizedDescription).")
} loadFriendsAuthorizationStatus
// Checking authorization
GKLocalPlayer.local.loadFriendsAuthorizationStatus { (authorizationStatus, error) in
guard error == nil else {
// Error handling
print(“Fail to load friends list with error: \(error!.localizedDescription).”)
return
}
// Handle GKFriendsAuthorizationStatus
switch authorizationStatus {
case .notDetermined:
// Player have not made a choice on friends list sharing
case .denied:
// Player have denied your request to access their friends list
case .restricted:
// You should delete collected player data from your end
case .authorized:
// Player have authorized your request to access their friends list
}
} loadFriends
func loadFriendsOnProgressionMap() async {
do {
let friends = try await GKLocalPlayer.local.loadFriends()
if friends.count > 0 {
let leaderboards = try await GKLeaderboard.loadLeaderboards(IDs: [“progress"])
if let leaderboard = leaderboards.first {
let entries = try await leaderboard.loadEntries(for: friends, timeScope: .allTime)
for entry in entries.1 {
let avatar = try await entry.player.loadPhoto(for: .normal)
let name = entry.player.displayName
let friendLevel = entry.score
// Display player on progression map
}
}
}
} catch {
print("Error: \(error.localizedDescription).")
}
} Enable Fast Start Mode
// Set canStartWithMinimumPlayers to true to enable Fast Start mode
let request = GKMatchRequest()
request.minPlayers = 2
request.maxPlayers = 6
request.playerGroup = 2021
let vc = GKMatchmakerViewController(matchRequest: request)
vc.canStartWithMinimumPlayers = true
vc.delegate = self
self.present(vc, animated: true, completion: nil) Handle Players Who Join The Game
// Set the GKMatch delegate and present your game scene when didFindMatch is called
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
viewController.dismiss(animated: true, completion: nil)
let gameVC = GameSceneViewController()
gameVC.match = match
match.delegate = gameVC
self.present(gameVC, animated: true, completion: nil)
}
// Add players who join later by implementing didChangeState delegate
func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
if state == .connected {
self.addPlayer(player)
}
} Present GKMatchmakerViewController on The Invitee Side
// On the invitee side, present GKMatchmakerViewController with the invite
func player(_ player: GKPlayer, didAccept invite: GKInvite) {
if let vc = GKMatchmakerViewController(invite: invite) {
vc.matchmakerDelegate = self
self.present(vc, animated: true, completion: nil)
}
} Resources
Related sessions
-
10 min -
26 min -
18 min -
25 min