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)
}
答案 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)
}