我发现相同代码的多个版本可以在 GameCenter 中验证用户身份,并且每个版本似乎都不想工作(在模拟器中打开应用程序时,绝对没有弹出任何内容,GameCenter 没有提示等)。这主要基于苹果在线的内容,但这是我的代码(请注意,我使用的取景器称为 AchievementsViewFinder,我只是想确保它出现在该页面上或当用户首次打开应用程序时,任一作品):
import Foundation
import GameKit
import UIKit
class AchievementsViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
authenticateUser()
}
private func authenticateUser(){
let player = GKLocalPlayer.local
player.authenticateHandler = { AchievementsViewController, error in
guard error == nil else{
print(error?.localizedDescription ?? ""); return
}
self.presentViewController(vc!, animated: true, completion: nil)
}
}
@IBAction func showAchievements(_ sender: Any) {
}
@IBAction func showLeaderboards(_ sender: Any) {
}
@IBAction func unlockAchievements(_ sender: Any) {
}
@IBAction func submit(_ sender: Any) {
}
}
我认为我调整取景器是导致我的代码无法工作的原因,但是在尝试了不同的解决方案(主要是在 stackoverflow 上)之后,我意识到很多人处理相同代码的方法有很大不同。任何帮助将不胜感激,即使它完全重写了我所拥有的。
1。身份验证处理程序实现
1。检查游戏中心设置
2。验证身份验证 UI 代码
3.模拟器检查
4。查看身份验证处理程序代码
GKLocalPlayer.local.authenticateHandler = { viewController, error in
if let viewController = viewController {
// Present the view controller so the player can sign in.
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.
}
// Perform any other configurations as needed (for example, access point).
}