Create the Game Center Helper

Apple Game Frameworks and Technologies — by Tammy Coron (155 / 193)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Add an Achievement | TOC | Show the Game Center Dashboard 👉

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…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.