14

I am using NSUbiquitousKeyValueStore to sync some preference data to iCloud. I found that if the user disable "Document & Data" item of iCloud in "Setting App", NSUbiquitousKeyValueStore can not synchronize its data to iCloud. So, I want to first check if this setting is switched on. I found this Code snippet:

    NSURL *ubiq = [[NSFileManager defaultManager] 
                   URLForUbiquityContainerIdentifier:nil];
    NSLog(@"url=%@",ubiq);
    if (!ubiq) {
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:nil message:@"Please enable iCloud in Setting app" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [av show];
        return;

    }

What I want to know is whether this is the only way to detect, even if I just use NSUbiquitousKeyValueStore (not iCloud document storage). Is there a better alternative?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jagie
  • 2,190
  • 3
  • 27
  • 25

2 Answers2

14

Basically, NSUbiquitousKeyValueStore is always available, even if the user does not have an iCloud account (in this case, NSUbiquitousKeyValueStore is just a simple local storage). So in general, you don't need to ask yourself or the user if there is a configured iCloud account.

If you really want to know for sure, in iOS 5, the only solution is the one you're mentioning.

Julien
  • 3,427
  • 20
  • 22
  • Your answer seems to be in conflict with the docs, which say: “Avoid using this class for data that is essential to your app’s behavior when offline […]”. I already posted a [question](http://stackoverflow.com/q/23662856/251760) about that. Would you be able to answer that? – Jonathan Oct 24 '14 at 16:33
  • What this means is that you should not rely on this store to be your local truth but more of a simple sync communication channel between your devices. – Julien Oct 24 '14 at 20:23
  • Thanks! I made your reply into an [answer](http://stackoverflow.com/a/26556553/251760) to my question, I hope you're fine with that. (If you'd rather make the answer yourself, I'd happily delete mine.) – Jonathan Oct 24 '14 at 21:30
5

I don't know if the docs have been updated, but Apple is now saying:

To determine if iCloud is available, especially at launch time, check the value of the ubiquityIdentityToken property instead

See NSFileManager for both ubiquityIdentityToken and URLForUbiquityContainerIdentifier.

Note also that Apple is saying that URLForUbiquityContainerIdentifier can be relatively slow, and that checking ubiquityIdentityToken is relatively fast.

This is for iOS6 and higher.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
Chris Prince
  • 7,288
  • 2
  • 48
  • 66
  • 2
    This true if you use iCloud Documents, the question is about the iCloud key-value storage. – Axel Guilmin Nov 01 '14 at 15:45
  • Hmmm. Am I not getting something? The docs for ubiquityIdentityToken don't suggest it is specific to iCloud Documents: "When iCloud is currently available, this property [i.e., ubiquityIdentityToken] contains an opaque object representing the identity of the current user. If iCloud is unavailable for any reason or there is no logged-in user, the value of this property is nil." – Chris Prince Nov 01 '14 at 19:15
  • 1
    As Julien said, the key-value storage is always available (even when `ubiquityIdentityToken` is nil). We want to check if the user disabled Settings > iCloud > Document & Data. – Axel Guilmin Nov 02 '14 at 17:43