2
votes

Trying to fix some issue with iCloud. Here are two versions of my code.

- (void)viewDidLoad{

    [super viewDidLoad];

    [self checkICloudData];
}

version 1

- (void)checkICloudData
{
    NSFileManager * fileManager=[NSFileManager defaultManager];

    NSURL *iCloudURL=[fileManager URLForUbiquityContainerIdentifier:nil];

    NSLog(@"iCloud URL is %@",[iCloudURL absoluteString]);

    if (iCloudURL){

        NSUbiquitousKeyValueStore * store=[NSUbiquitousKeyValueStore defaultStore];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(updateICloudData:)
                                                     name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
                                                   object:store];
        [store synchronize];
    }else{
        NSLog(@"iCloud is not supported or enabled");
        [self loadDataFromBundle];

    }
}

iCloudURL always returns nil. Other methods do not call.

version 2

- (void)checkICloudData
{   
    NSUbiquitousKeyValueStore * store=[NSUbiquitousKeyValueStore defaultStore];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateICloudData:)
                                                 name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
                                               object:store];
    [store synchronize];
}

- (void)updateICloudData:(NSNotification*)notification
{
    NSDictionary *userInfo = [notification userInfo];
    NSNumber *changeReason = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey];
    NSInteger reason = -1;

    if (!changeReason) {
        return;
    } else {
        reason = [changeReason integerValue];
    }

    if ((reason == NSUbiquitousKeyValueStoreServerChange) || (reason == NSUbiquitousKeyValueStoreInitialSyncChange)) {
        NSArray *changedKeys = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey];

        for (NSString *key in changedKeys) {
            if ([key isEqualToString:casesKey]) {
                [self iCloudNotification];
            }else {
                [self loadDataFromBundle];

            }
        }
    }
}

With that version iCloud sync works fine with the iPad, but doesn't work with the iPhone. In the iPhone's iCloud Drive settings i see my app same as in the iPad

My iCloud settings:

  1. Member center - iCloud Enabled. Compatibility - Include CloudKit support

  2. Target Capabilities - Services checked only Key-Value storage

  3. Created by default entitlements dictionary contains key com.apple.developer.icloud-container-identifiers with an empty array and key com.apple.developer.ubiquity-kvstore-identifier with a correct string value of my appID

So, why iCloudURL always returns nil? And why the second version works correct with the iPad but my iPhone does not see NSUbiquitousKeyValueStoreDidChangeExternallyNotification?

1

1 Answers

0
votes

Well, the problem was in my iPhone after upgrade iOS.

When I've delete iCloud profile from iPhone's settings and add it again, the iPhone started to sync with iCloud.

Right now the second version of the code works with both devices.

Hope it helps somebody to solve that kind of problem.