Is there a way for me to check and see if a user is logged into iCloud when they open the app up? I want to be able to direct them to the settings page if they are not logg
There are two methods for checking iCloud functionalities, which are provided for two different needs.
From Apples documentation:
FileManager.default.ubiquityIdentityToken -> An opaque token that represents the current user’s iCloud Drive Documents identity.
In iCloud Drive Documents, when iCloud is available, this property contains an opaque object representing the identity of the current user. If iCloud is unavailable or there is no logged-in user, the value of this property is nil.
To check for this iCloud functionality we can retrieve that token and check for nil.
// Request iCloud token
let token = FileManager.default.ubiquityIdentityToken
if token == nil {
print("iCloud (Drive) is not available")
} else {
print("iCloud (Drive) is available")
}
To assure beeing notified, if iCloudDrive availabilty changes during the app runs -> register to the NotificationCenter for NSUbiquityIdentityDidChange notification.
To check, wether the users iCloud account is available for accessing the CKContainer (and its private database), we can use an async request on the default container.
// Check iCloud account status (access to the apps private database)
CKContainer.default().accountStatus { (accountStatus, error) in
if accountStatus == .available {
print("iCloud app container and private database is available")
} else {
print("iCloud not available \(String(describing: error?.localizedDescription))")
}
}
To be informed about changes while the app is running, you can use the CKAccountChanged notification.