Dun*_*ald 34
你走了 - 希望自我解释.有关NSFileManager函数的Apple文档,请参阅下面的内容.
func isICloudContainerAvailable()->Bool {
if let currentToken = NSFileManager.defaultManager().ubiquityIdentityToken {
return true
}
else {
return false
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅下面的摘录:表示当前用户的iCloud身份的不透明令牌(只读)当iCloud当前可用时,此属性包含表示当前用户身份的不透明对象.如果iCloud因任何原因不可用或没有登录用户,则此属性的值为nil.
Jos*_*ffy 34
如果您只想知道用户是否登录到iCloud,可以使用同步方法:
if FileManager.default.ubiquityIdentityToken != nil {
print("iCloud Available")
} else {
print("iCloud Unavailable")
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您想知道为什么 iCloud不可用,您可以使用异步方法:
CKContainer.default().accountStatus { (accountStatus, error) in
switch accountStatus {
case .available:
print("iCloud Available")
case .noAccount:
print("No iCloud account")
case .restricted:
print("iCloud restricted")
case .couldNotDetermine:
print("Unable to determine iCloud status")
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想使用异步方法,但不关心为什么,你应该检查accountStatus的available,而不是检查,这不是noAccount:
CKContainer.default().accountStatus { (accountStatus, error) in
if case .available = accountStatus {
print("iCloud Available")
} else {
print("iCloud Unavailable")
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这种异步方法是首选,因此您在检查时不会阻止.
CKContainer.defaultContainer().accountStatusWithCompletionHandler { (accountStat, error) in
if (accountStat == .Available) {
print("iCloud is available")
}
else {
print("iCloud is not available")
}
}
Run Code Online (Sandbox Code Playgroud)
有两种方法可以检查 iCloud 功能,以满足两种不同的需求。
\n来自苹果文档:
\nFileManager.default.ubiquityIdentityToken ->代表当前用户\xe2\x80\x99s iCloud Drive Documents 身份的不透明令牌。
\n在 iCloud Drive 文档中,当 iCloud 可用时,此属性包含一个表示当前用户身份的不透明对象。如果 iCloud 不可用或没有登录用户,则该属性值为 nil。
\n要检查此 iCloud 功能,我们可以检索该令牌并检查nil。
\n// Request iCloud token\nlet token = FileManager.default.ubiquityIdentityToken\nif token == nil {\n print("iCloud (Drive) is not available")\n} else {\n print("iCloud (Drive) is available")\n}\nRun Code Online (Sandbox Code Playgroud)\n为了确保收到通知,如果 iCloudDrive 可用性在应用程序运行期间发生变化 -> 注册到通知中心以获取NSUbiquityIdentityDidChange通知。
\n要检查用户的 iCloud 帐户是否可用于访问CKContainer(及其私有数据库),我们可以在默认容器上使用异步请求。
\n// Check iCloud account status (access to the apps private database)\nCKContainer.default().accountStatus { (accountStatus, error) in\n\n if accountStatus == .available {\n print("iCloud app container and private database is available")\n } else {\n print("iCloud not available \\(String(describing: error?.localizedDescription))")\n } \n}\nRun Code Online (Sandbox Code Playgroud)\n要在应用程序运行时了解更改,您可以使用CKAccountChanged通知。
\n