游戏中心未经身份验证,在iOS 12中请求身份验证

时间:2019-01-02 15:47:12

标签: ios swift game-center

Swift 4,iOS 12。

使用Game Center在游戏中记录高分。假设用户已登录Game Center,一切正常。我可以检测出他们是否还没有,但是无法弄清楚如何打开Game Center。

func ask4GameCenter() {
    let myAlert: UIAlertController = UIAlertController(title: "Attention", message: "Log into Game Center to record High Scores", preferredStyle: .alert)

    myAlert.addAction(UIAlertAction(title: "Ignore", style: .default, handler: { (action) in
        self.gameOn()
    }))
    myAlert.addAction(UIAlertAction(title: "Logon", style: .default, handler: { (action) in
        UIApplication.shared.open(NSURL(string: "gamecenter:")! as URL, options: [:], completionHandler: { (success) in
            if success {
                self.gameOn()
            }
        })
    }))
    self.view?.window?.rootViewController?.present(myAlert, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

好,我找到了答案。

func ask4GameCenter() {
    let myAlert: UIAlertController = UIAlertController(title: "Attention", message: "Log into Game Center to record High Scores", preferredStyle: .alert)

    myAlert.addAction(UIAlertAction(title: "Ignore", style: .default, handler: { (action) in
        self.gameOn()
    }))
    myAlert.addAction(UIAlertAction(title: "Logon", style: .default, handler: { (action) in
        guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
            return
        }
        UIApplication.shared.open(settingsUrl, options: [:], completionHandler: { (success) in
            if success {
                self.gameOn()
            }
        })
    }))
    self.view?.window?.rootViewController?.present(myAlert, animated: true, completion: nil)
}