[SWIFT] Implementation of GKAccessPoint

Introduction

From iOS14, you can use GKAccessPoint with GameKit. It's like a button that gives you one-tap access to Leaderboards for rankings and Achievements for in-game achievements. It is recommended because it is very easy to install.

What is GKAccessPoint

Below is something like the icon in the upper left of the image. It can be installed in any of the four corners of the screen, and in my case it is installed in the upper left. Apple recommends installing it in the upper left corner as much as possible. 3E821B2A-5646-44C3-BA66-20FDEE20D46D.png

When you tap Access Point, the following Game Center screen will be displayed. This menu screen is called Dashboard. The items displayed on the Dashboard change depending on the Game Center settings in AppStore Connect. You can also display Leaderboards and user profiles directly without going through the Dashboard. 9B7AB038-B1AB-4022-AECE-DEF38B93320D.png

Precautions before introduction

GKAccessPoint has a Bool-type property called .showHighlights. If this value is set to true, each time the Access Point is displayed, the following highlights about the user's current ranking and the number of achievements earned will be displayed for a few seconds. So if you set .showHighlights to true, you need to be careful that the display area of ​​this highlight does not cover the game screen UI. BC218BFA-0A99-42AB-A90F-D6584DFED355_1_102_o.jpeg

In the case of horizontal screen, the number of points of Access Point is as follows. (For iPhone 11 Pro) It is recommended to leave a safe area of ​​91pt with a height of 62pt and a width of 280pt. By the way, in the case of vertical screen, it is recommended to have 62pt in height and 335pt in width, and 114pt in safe area. Image.001.png

Implementation method

Register your app in AppStore Connect and turn on the Game Center feature

Obviously, it's a Game Center feature, so After registering your app in AppStore Connect, select the Game Center checkbox. By the way, it's a good idea to add Leaderboards and Achievements in Features-> Game Center.

Xcode settings

Turn on Project-> General-> Capabilities-> Game Center Added GameKit.Framework in Project-> General-> Linked Frameworks and Libraries

Game Center login process

Access Points cannot be used unless the user is logged in to Game Center. Therefore, it is necessary to complete the login process to Game Center immediately after starting the application. Since it is not directly related to GKAccessPoint, I will omit the explanation and put only the code. I wrote as follows. If the user is not signed in to Game Center A view controller dedicated to sign-in is entered in the argument viewController of GKLocalPlayer.local.authenticateHandler, so display it. If the user has turned off the Game Center feature in the settings in the first place, an error will occur. If you sign in successfully, Game Center will authenticate you for you, so you don't have to do anything.

GameCenterLocalPlayer.swift


struct GameCenterLocalPlayer {

   static func loginGameCenter(target: UIViewController) {
        GKLocalPlayer.local.authenticateHandler = { viewController, error in
            if error != nil {
                let alert = UIAlertController(title: "Game Center", message: "To join the ranking,\nPlease sign in to GameCenter.", preferredStyle: .alert)
                let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                alert.addAction(alertAction)
                target.present(alert, animated: true)
            }
            if let _viewController = viewController {
                target.present(_viewController, animated: true)
            }
        }
    }

}

AppDelegate.swift


class AppDelegate: UIResponder, UIApplicationDelegate {

   var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        self.window?.makeKeyAndVisible()
        
        if let presentView = window?.rootViewController {
            GameCenterLocalPlayer.loginGameCenter(target: presentView)
        }
        
        return true
    }

}

Write code for GKAccessPoint

Finally call GKAccessPoint. As you can see, the call is made via a shared singleton called shared. Apple recommends calling Access Point in the game's main menu if possible. I was using SpriteKit, so I wrote it in the didMove method of the menu Scene. Note that once you call the AccessPoint, it will not disappear even if you straddle the Scene, so for example, on the game play screen, set isActive to false and delete the AccessPoint.

HogehogeScene.swift


override func didMove(to view: SKView) {
        GKAccessPoint.shared.location = .topLeading
        GKAccessPoint.shared.showHighlights = true
        GKAccessPoint.shared.isActive = true
}

that's all.

Reference site

https://developer.apple.com/videos/play/wwdc2020/10618

Recommended Posts

Implementation of GKAccessPoint
Implementation of flash messages
Implementation of search function
Applied implementation of chat-space
Implementation of pagination function
Rails implementation of ajax removal
Implementation of sequential search function
Implementation of like function (Ajax)
[Rails 6] Implementation of search function
Implementation of image preview function
[Java] Implementation of Faistel Network
Implementation of XLPagerTabStrip with TabBarController
[Rails] Implementation of category function
Implementation of category pull-down function
Implementation of unit test code
Implementation of gzip in java
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Implementation of tri-tree in Java
Implementation of HashMap in kotlin
[Rails] Implementation of user logic deletion
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
Implementation of ls command in Ruby
Easy implementation of Android file browsing
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Implementation of asynchronous processing in Tomcat
Implementation of validation using regular expressions
[Rails] Implementation of many-to-many category functions
Default implementation of Object.equals () and Object.hashCode ()
Implementation of like function in Java
Implementation of clone method for Java Record
Implementation of DBlayer in Java (RDB, MySQL)
Whereabouts of JAXB Reference implementation and DatatypeConverterImpl
Implementation of user authentication function using devise (2)
[Swift 5] Implementation of membership registration with Firebase
Implementation of multi-tenant asynchronous processing in Tomcat
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of tabs using TabLayout and ViewPager
Implementation of user authentication function using devise (3)
12 of Array
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Implementation] Eliminate the ominous smell of code
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
iOS app development: Timer app (8. Implementation of progress bar)
Do you need a memory-aware implementation of Java?
[FCM] Implementation of message transmission using FCM + Spring boot
Easy way to create an implementation of java.util.stream.Stream
[Rails] Implementation of image enlargement function using lightbox2
Implementation of digit grouping in flea market apps
SKStoreReviewController implementation memo in Swift UI of iOS14
[Rails] Implementation of retweet function in SNS application