Authenticating the player's Game Center account
As soon as our app starts, we will check if the player is already logged in to their Game Center account. If not, we will give them a chance to log in. Later, when we want to submit high scores or achievements, we can use the authentication information we gathered when the app launched, instead of interrupting their gaming session to collect their Game Center information.
Follow these steps to authenticate the player's Game Center account when the app starts:
- First, we will turn on Game Center for your project in Xcode. Open the Capabilities tab of your project settings and make sure Game Center is flipped to ON, as shown in the following screenshot:
- We will now be working in the
GameViewController
class, so openGameViewController.swift
in Xcode. - Add a new
import
statement at the top of the file so we can use theGameKit
framework:import GameKit
- In the
GameViewController
class, add a new function calledauthenticateLocalPlayer...