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:
We will 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
with this code:// (We pass in the menuScene instance so we can create a // leaderboard button in the menu when the player is // authenticated with Game Center) func authenticateLocalPlayer(menuScene:MenuScene) { ...