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?
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
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.
func isUserSignedIntoICloud() -> Bool {
return FileManager.default.ubiquityIdentityToken != nil
}