Create the Game Center Helper

Switch back to the Xcode hog project, and in the Project Navigator, just below the Assets.xcassets asset catalog, create a new group (N). Name this group Game Center; this is where you’ll put all of your Game Center–related code.

Inside the new group, create a new file (N) using the iOS Swift File template. Name the new file GameKitHelper.swift and replace its contents with the following code:

 import​ ​GameKit
 
 class​ ​GameKitHelper​: ​NSObject​ {
 
 // Shared GameKit Helper
 static​ ​let​ shared: ​GameKitHelper​ = {
 let​ instance = ​GameKitHelper​()
 
 return​ instance
  }()
 }

This code is the start of your new GameKit helper class; it sets up the shared instance property, which you’ll use to access the main GameKit methods.

The next step is to authenticate the local player on the device so that they can access the Game Center features of your game.

Authenticating the Local Player

Before you can use the Game Center features, you need to authenticate the local Game Center player—and this process should happen as early as possible.

Authentication happens by setting the authentication handler on the shared instance of the GKLocalPlayer object. This object represents the authenticated Game Center player on the device. For this process, you’ll use the Notification Center. With notifications,[53] you can broadcast specific information to designated observers. These observers then watch for the notifications, taking specific actions when they occur.

In this case, you’ll create a custom notification that informs its observers that it’s time to authenticate the player.

At the bottom of the GameKitHelper.swift file (outside the GameKitHelper class), add the following code:

 // MARK: - NOTIFICATION EXTENSIONS
 
 extension​ ​Notification​.​Name​ {
 static​ ​let​ presentAuthenticationViewController =
 Notification​.​Name​(​"presentAuthenticationViewController"​)
 }

The next step is to set up the property that’ll hold the authentication view controller object.

Inside the GameKitHelper class, add the following new property:

 // Game Center- & GameKit-Related View Controllers
 var​ authenticationViewController: ​UIViewController​?

And now for the fun part: adding the method that handles authentication.

Below the new authenticationViewController property, add the following code:

 // MARK: - GAME CENTER METHODS
 
 func​ ​authenticateLocalPlayer​() {
 
 // Prepare for new controller
  authenticationViewController = ​nil
 
 // Authenticate local player
 GKLocalPlayer​.local.authenticateHandler = { viewController, error ​in
 
 if​ ​let​ viewController = viewController {
 // Present the view controller so the player can sign in
 self​.authenticationViewController = viewController
 NotificationCenter​.​default​.​post​(
  name: .presentAuthenticationViewController,
  object: ​self​)
 return
  }
 
 if​ error != ​nil​ {
 // Player could not be authenticated
 // Disable Game Center in the game
 return
  }
 
 // Player was successfully authenticated
 // Check if there are any player restrictions before starting the game
 
 if​ ​GKLocalPlayer​.local.isUnderage {
 // Hide explicit game content
  }
 
 if​ ​GKLocalPlayer​.local.isMultiplayerGamingRestricted {
 // Disable multiplayer game features
  }
 
 if​ ​GKLocalPlayer​.local.isPersonalizedCommunicationRestricted {
 // Disable in game communication UI
  }
 
 // Place the access point in the upper-right corner
 // GKAccessPoint.shared.location = .topLeading
 // GKAccessPoint.shared.showHighlights = true
 // GKAccessPoint.shared.isActive = true
 
 // Perform any other configurations as needed
  }
 }

There’s a lot of code you’re not using here, but it’s code that you should know about in case you want or need to include these features in your own games; for example, you may need to restrict certain content—like multiplayer gaming or personalized communication—based on the player’s Game Center preferences. Or, you may decide to include the Game Center Access Point[54] if your game’s design allows for it. The Game Center Access Point is an Apple-designed interface that lets players view their Game Center profiles without leaving your game. Deciding to use (or not use) the access point depends on your game’s design and, perhaps, how important the Game Center features are to its players. Hog Dice isn’t designed to use this access point, so the code is commented out.

Now, focus on the first block of code. Notice that it’s setting the authenticationViewController property and then posting the .presentAuthenticationViewController notification. This notification is what your observers will be looking for and is the mechanism you’ll use to present the authentication controller.

Presenting the Authentication Controller

To present the Game Center authentication controller, you’ll configure the main view controller to act as an observer. Switch to the GameViewController.swift file. Near the end of the file, replace the line that reads // TODO: Add Game Center Notification Handlers with the following code:

 @objc​ ​func​ ​showAuthenticationViewController​() {
 if​ ​let​ viewController = ​GameKitHelper​.shared.authenticationViewController {
 present​(viewController, animated: ​true​, completion: ​nil​)
  }
 }

This is the code you’ll call when the .presentAuthenticationViewController notification gets posted; it handles presenting the shared view controller object stored in the GameKitHelper class.

All that’s left to do now is set up the GameViewController class as an observer of that notification and call the authenticateLocalPlayer() method in the GameKitHelper class.

In the viewDidLoad() method located at the top of the file, replace the two TODO comments with the following code:

 // Add Game Center Observers
 NotificationCenter​.​default​.​addObserver​(
 self​,
  selector: ​#selector(​​self.showAuthenticationViewController​​)​,
  name: .presentAuthenticationViewController, object: ​nil​)
 
 // Authenticate the Local GC Player
 GameKitHelper​.shared.​authenticateLocalPlayer​()

Build and run the project.

images/AddingLeaderboardsAndAchievements/build-gc-prompt.png

The first time you run the game, you’ll be prompted to log in to the Game Center. Be sure to log in with your sandbox tester account. After a successful login, stop the project.

Build and run the project again.

images/AddingLeaderboardsAndAchievements/build-gc-authenticated.png

Notice that this time around, the player is already logged in and authenticated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset