Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 604
tomDev Avatar asked Oct 17 '14 15:10

tomDev


4 Answers

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.

like image 127
r-dent Avatar answered Nov 12 '22 17:11

r-dent


I have had the same problem, I solved it by:

  1. Uninstall app
  2. restart device
  3. install app

rather than sign in and out of iCloud.

like image 29
user6821300 Avatar answered Nov 12 '22 16:11

user6821300


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];

like image 1
Peter B. Kramer Avatar answered Nov 12 '22 18:11

Peter B. Kramer


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.

like image 1
Kalzem Avatar answered Nov 12 '22 17:11

Kalzem