Is it possible to check if the user is connected to his Apple Account ? And if he is not, ask him to connect ?
3 Answers
Yes it is possible to get Apple account (iCloud) status by checking if the ubiquityIdentityToken object in NSFileManager is set. You can use something like this:
Swift 4
var iCloudAvailability: Bool {
return FileManager.default.ubiquityIdentityToken != nil
}
Swift 3
var icloudStatus: Bool {
return NSFileManager.defaultManager().ubiquityIdentityToken != nil ? true : false
}
Value of true would indicate that user is logged in, and false that user is not logged in, so you can present some kind of view to ask user to connect.
From the Apple documentation:
When iCloud is currently available, this property contains an opaque object representing the identity of the current user. If iCloud is unavailable for any reason or there is no logged-in user, the value of this property is nil. Accessing the value of this property is relatively fast so you can check the value at launch time from your app’s main thread.
If you are checking for iCloud services then from CloudKit you can use CKContainer class to get user account status as below
CKContainer *container = [CKContainer defaultContainer];
[container accountStatusWithCompletionHandler:^(CKAccountStatus accountStatus, NSError * _Nullable error) {
//Here goes your code to handle the account status
}];
iOS 10.x swift 4.0 Based on the Said Sikira answer
func icloudStatus() -> Bool?{
return FileManager.default.ubiquityIdentityToken != nil ? true : false
}