因此,当我遇到CKErrorCode.NotAuthenticated
问题时,我使用以下代码行切换到设置:
let url = NSURL(string: UIApplicationOpenSettingsURLString)
let result = UIApplication.sharedApplication().openURL(url)
我将立即在上下文中显示代码。一开始似乎很好(我的应用程序进入后台),但后来我又回到了我的应用程序的新版本。后退按钮展开到设置(相应地标记),然后我可以从那里展开回到原始应用程序。除了从“设置”打开的应用程序的迭代无法加载任何内容(它的标题是空白屏幕)之外,其他一切都正常工作。
这是我第一次创建 XIB,因此可能需要牢记这一点。我在以下情况下触发的 switch 语句中进行错误处理:
case CKErrorCode.NotAuthenticated.rawValue:
//Should check account status here...
// If account not logged in...
let message = "Your device needs to be logged in to your iCloud Account in order for Voyager to work correctly. After clicking 'OK' you can login at Settings/iCloud..."
presentAlertMessageToUser(message) { action in
if let goToSettingsURL = NSURL(string: UIApplicationOpenSettingsURLString) {
print("E3")
dispatch_async(dispatch_get_main_queue()) {
let result = UIApplication.sharedApplication().openURL(goToSettingsURL)
print("E4: \(result)")
}
}
}
正如你所看到的,我UIAlertViewController
在一个名为presentAlertMessageToUser(message: String?, handler: ((UIAlertAction) -> Void)?)
. 它本身似乎没有任何问题,但您可以在这里看到:
private func presentAlertMessageToUser(message: String?, handler: ((UIAlertAction) -> Void)?) {
print("G0")
let alert = UIAlertController(title: "Connection Error", message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
if let completionBlock = handler {
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: completionBlock))
} else {
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
}
dispatch_async(dispatch_get_main_queue()) {
print("G2")
unowned let me = self
me.presentViewController(alert, animated: true, completion: nil)
}
print("G1")
}
对于 switch 语句中的这种错误情况,我禁用了重试尝试,并且我还验证了它没有多次执行序列的任何部分。如果代码的任何其他部分有帮助,我可以将它与输出一起发布到我的控制台,在那里我使用这些打印行来监控应用程序的进度。