如何检查iCloud是否以编程方式配置

Bar*_*cki 15 iphone icloud

以下是来自Apple Docs的句子:"如果未配置iCloud,请询问用户是否要配置它(如果他们想要配置iCloud,最好将它们传输到启动设置)."

如何检查是否配置了iCloud以及如何启动iCloud的设置?

nac*_*o4d 27

编辑:
如果您的目标是iOS6或更高版本,则可以使用[[NSFileManager defaultManager] ubiquityIdentityToken];.有关用法示例,请参阅 @Dj S'回答 :).
它比原始解决方案更快更容易,原始解决方案适用于针对iOS5及更高版本的用户

原始答案
iOS应用程序编程指南中所述 - iCloud Storage.这可以通过向文件管理器询问ubiquity容器URL来检查:)

只要您在下面提供有效的普遍容器标识符,方法应该返回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;
}
Run Code Online (Sandbox Code Playgroud)

但是,我发现URLForUbiquityContainerIdentifier:在一个会话中第一次可能需要几秒钟(我在iOS5中使用它,所以现在可能会有所不同).我记得用过这样的东西:

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];
       }
   });
});
Run Code Online (Sandbox Code Playgroud)


Dj *_*j S 17

只是为了补充上面的答案,如果您只想知道iCloud是否适用于您的应用程序,例如
1.没有设置iCloud帐户,或
2.文档和数据被禁用(适用于所有应用程序),或
3.文档和数据仅针对您的应用停用

那么你可以使用NSFileManager's ubiquityIdentityTokeniOS 6以上.
如果value为nil,则表示未配置iCloud帐户.否则,配置iCloud帐户.

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

请注意,根据Apple文档,您可以从主线程调用它.

因为此方法返回相对较快,您可以在启动时调用它,并且可以从应用程序的主线程调用它.