【问题标题】:How to check if iCloud is configured programmatically如何检查 iCloud 是否以编程方式配置
【发布时间】:2011-12-10 19:41:37
【问题描述】:

这是 Apple Docs 中的一句话: “如果未配置 iCloud,请询问用户是否要配置它(如果他们要配置 iCloud,最好将其转移到启动设置)。”

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

【问题讨论】:

    标签: iphone icloud


    【解决方案1】:

    编辑:
    如果您的目标是 iOS6 或更高版本,您可以使用[[NSFileManager defaultManager] ubiquityIdentityToken];。用法示例请参考@Dj S' answer :)。
    它比针对 iOS5 及更高版本的人的原始解决方案更快、更容易

    原答案
    iOS App programming guide - iCloud Storage 中所述。这可以通过向文件管理器询问无处不在的容器 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;
    }
    

    但是,我发现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];
           }
       });
    });
    

    【讨论】:

    【解决方案2】:

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

    那么您可以将NSFileManager's ubiquityIdentityToken 用于iOS 6 及更高版本
    如果值为 nil,则未配置 iCloud 帐户。否则,配置 iCloud 帐户。

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

    注意,根据Apple docs,可以从主线程调用。

    因为这个方法返回比较快,所以你可以在启动时调用它,也可以从你应用的主线程调用它。

    【讨论】:

      猜你喜欢
      • 2015-02-18
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2013-10-23
      相关资源
      最近更新 更多