title: iCloud key-value sync iOS8 Xcode 6
body: <p>Trying to fix some issue with iCloud. Here are two versions of my code.</p>
  
  <pre><code>- (void)viewDidLoad{
  
      [super viewDidLoad];
  
      [self checkICloudData];
  }
  </code></pre>
  
  <p>version 1</p>
  
  <pre><code>- (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];
  
      }
  }
  </code></pre>
  
  <p>iCloudURL always returns nil. Other methods do not call.</p>
  
  <p>version 2</p>
  
  <pre><code>- (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];
  
              }
          }
      }
  }
  </code></pre>
  
  <p>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 </p>
  
  <p>My iCloud settings:</p>
  
  <ol>
  <li><p>Member center - iCloud Enabled. Compatibility - Include CloudKit support</p></li>
  <li><p>Target Capabilities - Services checked only Key-Value storage</p></li>
  <li><p>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</p></li>
  </ol>
  
  <p>So, why iCloudURL always returns nil? And why the second version works correct with the iPad but my iPhone does not see NSUbiquitousKeyValueStoreDidChangeExternallyNotification?</p>