在将数据保存到 CloudKit 之前,如何检查用户是否有帐户并已登录?
最佳答案
您需要使用 CKContainer
类中的 accountStatusWithCompletionHandler
并检查返回的 accountStatus
。
这在 CloudKit Quick Start 中有描述文档。
这是示例的 Swift 版本:
CKContainer.defaultContainer().accountStatusWithCompletionHandler { accountStatus, error in
if accountStatus == .NoAccount {
let alert = UIAlertController(title: "Sign in to iCloud", message: "Sign in to your iCloud account to write records. On the Home screen, launch Settings, tap iCloud, and enter your Apple ID. Turn iCloud Drive on. If you don't have an iCloud account, tap Create a new Apple ID.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
// Code if user has account here...
}
}
关于ios - 如何在保存数据之前检查用户是否登录到 iCloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39060513/