Search code examples
iphoneicloud

How to check if iCloud is configured programmatically


Here is the sentence from Apple Docs: "If iCloud is not configured, ask users if they want to configure it (and, preferably, transfer them to Launch Settings if they want to configure iCloud)."

How can I check if iCloud is configured or not and how to launch settings for iCloud?


Solution

  • Edit:
    If you are targeting iOS6 or above you can use [[NSFileManager defaultManager] ubiquityIdentityToken];. For usage example please refer @Dj S' answer :).
    It is faster and easier than the original solution which was meant for people targeting iOS5 and above

    Original Answer
    As documented in iOS App programming guide - iCloud Storage. That can be checked by asking the ubiquity container URL to the file manager :)

    As long as you supply a valid ubiquity container identifier below method should return YES

    - (BOOL) isICloudAvailable
    {
        // Make sure a correct Ubiquity Container Identifier is passed
        NSURL *ubiquityURL = [[NSFileManager defaultManager] 
            URLForUbiquityContainerIdentifier:@"ABCDEFGHI0.com.acme.MyApp"];
        return ubiquityURL ? YES : NO;
    }
    

    However, I've found that URLForUbiquityContainerIdentifier: might take several seconds the very first time within a session (I used it in iOS5 so things might be different now). I remember using something like this:

    dispatch_queue_t backgroundQueue = 
       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(backgroundQueue,^{
       BOOL isAvailable = [self isICloudAvailable]
      /* change to the main queue if you want to do something with the UI. For example: */
       dispatch_async(dispatch_get_main_queue(),^{
           if (!isAvailable){
             /* inform the user */
             UIAlertView *alert = [[UIAlertView alloc] init...]
             [alert show];
             [alert release];
           }
       });
    });