My question is similar to this: How to detect if iCloud account on a device changed?
Apple docs says:
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector (iCloudAccountAvailabilityChanged:)
name: NSUbiquityIdentityDidChangeNotification
object: nil];
But I need a little bit more information about implementing correctly the 'iCloudAccountAvailabilityChanged:
' method, because mine is never called, instead crashes my app.
Any answers would be greatly appreciated!
EDIT: I have in my didFinishLaunchingWithOptions:
self.currentiCloudToken= [NSFileManager defaultManager].ubiquityIdentityToken;
if(self.currentiCloudToken)
{
NSData *newTokenData=[NSKeyedArchiver archivedDataWithRootObject: self.currentiCloudToken];
[[NSUserDefaults standardUserDefaults] setObject: newTokenData forKey: @"com.apple.MyWishList.UbiquityIdentityToken"];
NSLog(@"You're logged in to iCloud! (from currentiCloudToken)");
[[CKContainer defaultContainer]fetchUserRecordIDWithCompletionHandler:^(CKRecordID *recordID, NSError *error) {
if(!error){
NSLog(@"eltaroltam a usert: %@", recordID.recordName);
[[NSUserDefaults standardUserDefaults]setValue:recordID.recordName forKey:@"userRecordID"];
[self firstLaunch];
}
else
NSLog(@"Probably not connected to internet:%@", [error localizedDescription]);
}];
}
else
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey: @"com.apple.MyWishList.UbiquityIdentityToken"];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Info" message:@"In order to share your wishes, you need to sign in to iCloud" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}