tomDev
tomDev

Reputation: 5760

NSUbiquitousKeyValueStore not syncing with iCloud

I've tried everything and read all topics, but I can't find out why NSUbiquitousKeyValueStore is not being stored on iCloud.

• Created a specific App ID

• Enabled iCloud to this App ID

• Created Provisioning Profile

• Enable iCloud on the Project

• Setup the entitlements to Key Value: $(TeamIdentifierPrefix)$(CFBundleIdentifier)

• Turned on iCloud Drive on the device

But NSUbiquitousKeyValueStore is only saving locally. When I reinstall the app it doesn't get info from iCloud.

This is how I'm trying:

    NSUbiquitousKeyValueStore *cloudStore = [NSUbiquitousKeyValueStore defaultStore];

if ([[cloudStore stringForKey:@"testString"] length] == 0) {
    NSLog(@"Nothing in iCloud - setting a value...");
    [cloudStore setString:@"I'm live in iCloud!" forKey:@"testString"];
    [cloudStore synchronize];
    [[NSUbiquitousKeyValueStore defaultStore] synchronize];

} else {
    NSString *result = [cloudStore stringForKey:@"testString"];
    NSLog(@"Found something in iCloud - here it is: %@", result);
}

[self registerForiCloudNotificatons];

If I delete the app or try on a new device it doesn't find anything on iCloud.

So I tried this to find out if iCloud is working:

    NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
    NSLog(@"iCloud at %@", ubiq);

} else {
    NSLog(@"No iCloud access, %@", ubiq);
}

And this always return "no iCloud access" and I can't figure it out why. No matter what I do URLForUbiquityContainerIdentifier always return nil.

I'm running on iOS 8 devices with iCloud enabled.

Am I missing something?

Thanks!

Upvotes: 8

Views: 3965

Answers (4)

user6821300
user6821300

Reputation: 111

I have had the same problem. I solved it like this:

  1. Uninstall the app
  2. Restart the device
  3. Install the app

No need to sign in and out of iCloud.

Upvotes: 7

Kalzem
Kalzem

Reputation: 7492

Just spent hours trying everything (Restart, clean build, uninstall, revoke, recreate entitlements, ...).

Then I realized that, next to the "+ Capabilities" there are tabs "All, Debug, Release" and if you added the capability under either Debug or Release then it ONLY works for that environment.

Screenshot of capabilities environment

You MUST create the capability under "All" tab. If you have previously created it under Debug or Release, make sure to delete that capability first before re-adding it.

Upvotes: 1

Peter B. Kramer
Peter B. Kramer

Reputation: 16563

You seem to be adding the observer after downloading the data. That may be wrong.

I have found that when the app is deleted and reinstalled from Xcode then, if you try to read from [NSUbiquitousKeyValueStore defaultStore] too quickly (e.g. from didFinishLaunchingWithOptions:) then you get nothing. If you have added an observer for NSUbiquitousKeyValueStoreDidChangeExternallyNotification then that observer is called with:

[[[notification userInfo] objectForKey: NSUbiquitousKeyValueStoreChangeReasonKey] intValue] == NSUbiquitousKeyValueStoreInitialSyncChange

From there you can read successfully from [NSUbiquitousKeyValueStore defaultStore].

An advantage of this is that the observer can also respond to NSUbiquitousKeyValueStoreChangeReasonKey] intValue] == NSUbiquitousKeyValueStoreServerChange

and the device will get changes made by any other device shortly after that other device does a [[NSUbiquitousKeyValueStore defaultStore] synchronize];

Upvotes: 1

r-dent
r-dent

Reputation: 697

I had the same problem. Logging off and back on my iCloud account on the device solved it for me, too.

This problem seems to appear when you have installed the app with the same bundle identifier on the device before you enabled iCloud entitlements. I had this problem on 2 testing devices. A third device on which i installed the app the first time with iCloud enabled, made no problems.

Upvotes: 7

Related Questions