It enables users to track their high scores on a leaderboard, compare achievements, invite friends to play a game, and to start a multiplayer game through auto-matching. So if you have built an iOS game and want users to be able to post their scores to a Game Center leaderboard, this is the right tutorial for you! For more info about apps and the Game Center, visit its Apple Developer page. I'll explain all the necessary steps to create a leaderboard on iTunes Connect and the needed code to submit a score to it, along with a button to open the leaderboard from within the app.
Here's how the app's Storyboard looks: You can paste this code right into your ViewController class: That's why I've identified this leaderboard as com. Before enabling Game Center in the iTunes Connect page of your app, let's finish the basic code.
Add this line into viewDidLoad: Show login if player is not logged in self. Game center is not enabled on the users device self. Once the player is logged in, the app gets the default leaderboard id. Now we're done coding, but you can't run the app yet. Choose Single Leaderboard on the next screen. Here you have to type the name you want to give your leaderboard.
You may call yours Best Score Leaderboard or anything you wish. Since the score is a number, select Integer in the Score Format Type field. You may choose the option you want for Submission Type and Sort Order. Leave Score Range Optional blank. In the popup window, you must select the language of your leaderboard. The default is always English. You can also add an icon.
Click Choose File to upload your image. Lastly click Save, and you're done. Once you've added a window, you can check over your leaderboard's details. If everything is fine, click Save, and you'll be redirected to the Features page, with your brand new leaderboard. Now it's time to enable Game Center in the App Store section of your app. Click App Store and Prepare for Submission. Scroll down until you find Game Center with a switch next to it. Enable this, and it will turn green.
Click Save in the top-right corner of the window, and you'll be all done setting up Game Center on iTunes Connect. You can now go back to your Xcode project and run the app on a real device or even on the iOS Simulator. It looks like this: Sign in with your credentials, and you can start testing the app!
Tap the red button three more times, and then tap the blue one to open your leaderboard, and check that the submitted score is You should see something like this: It's a template for a minimal endless game that saves best scores and submits them to the Game Center. Game templates like this are a great way to get a head start on your next game. CodeCanyon has hundreds of iOS game templates that you can use to jump-start development—letting you build the next killer game that much faster!
Thanks for reading, and I'll see you next time! Please check out some of our other tutorials on Swift and iOS app development.