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.
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.
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.
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.
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.
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.
Turn on Project-> General-> Capabilities-> Game Center Added GameKit.Framework in Project-> General-> Linked Frameworks and Libraries
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
}
}
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.
https://developer.apple.com/videos/play/wwdc2020/10618
Recommended Posts