How to detect if a user is signed in to iCloud?

纵然是瞬间 提交于 2019-12-10 14:28:45

问题


How would I detect specifically if a user is signed into iCloud? Checking the return value of URLForUbiquityContainerIdentifier is very limited:

1) It can't distinguish between a user not being signed into iCloud versus other scenarios, e.g. iCloud not enabled. 2) It doesn't appear to change its behavior if a user signs into iCloud from the Settings app while the game is still running in the background, then returns to the app to try to access iCloud services.

Is there a more reliable way to detect specifically if a user is signed in?


回答1:


Also, from iOS 6 on there is the method [[NSFileManager defaultManager] ubiquityIdentityToken], which tells you if iCloud is available and also if the user has switched accounts




回答2:


There's no way to distinguish "user is not logged in" from "iCloud is not enabled". What you get to know in your app is whether your app currently has access to iCloud, but if the answer is no then you don't get to know why.

However there is a solution to your issue #2-- Observe NSUbiquityIdentityDidChangeNotification. It fires any time the overall iCloud state changes-- user logs in, user logs out, user switches accounts.




回答3:


func isUserSignedIntoICloud() -> Bool {
    return FileManager.default.ubiquityIdentityToken != nil
}


来源:https://stackoverflow.com/questions/11509601/how-to-detect-if-a-user-is-signed-in-to-icloud

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!