如何跟踪iCloud的变化?

时间:2014-11-04 08:29:11

标签: ios xcode icloud

我的问题与此类似: How to detect if iCloud account on a device changed?

Apple文档说:

[[NSNotificationCenter defaultCenter]
    addObserver: self
       selector: @selector (iCloudAccountAvailabilityChanged:)
           name: NSUbiquityIdentityDidChangeNotification
         object: nil];

但是我需要更多关于正确实现'iCloudAccountAvailabilityChanged:'方法的信息,因为我从未调用过,而是崩溃了我的应用。

任何答案都将非常感谢!

编辑: 我有我的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];
}

0 个答案:

没有答案