Book Image

Game Development with Swift

By : Stephen Haney
Book Image

Game Development with Swift

By: Stephen Haney

Overview of this book

Table of Contents (18 chapters)
Game Development with Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. We will be working in the GameViewController class, so open GameViewController.swift in Xcode.

  2. Add a new import statement at the top of the file so we can use the GameKit framework:

    import GameKit
  3. In the GameViewController class, add a new function called authenticateLocalPlayer 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) {
    ...