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?

有帮助吗?

解决方案

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

其他提示

Just to supplement the answer above, if you only want to know if iCloud is available for your application, e.g.
1. no iCloud account is setup, or
2. Documents and Data is disabled (for all apps), or
3. Documents and Data is disabled for your app only

then you can use NSFileManager's ubiquityIdentityToken for iOS 6 and above.
If value is nil, then iCloud account is not configured. Otherwise, iCloud account is configured.

id token = [[NSFileManager defaultManager] ubiquityIdentityToken];
if (token == nil)
{
    // iCloud is not available for this app
}
else
{
    // iCloud is available
}

Note that according to Apple docs, you can call it from the main thread.

Because this method returns relatively quickly, you can call it at launch time and you can call it from your app’s main thread.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top