2011-10-19 84 views

回答

27

編輯:
如果你是針對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]; 
     } 
    }); 
}); 
+0

優秀的解決方案。 – NSSplendid

+1

不幸的是,鏈接到Apple文檔不起作用。您可以在這裏搜索URLForUbiquityContainerIdentifier:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html – Oscar

+0

Apple不斷更改位置,鏈接已更新。 :) – nacho4d

17

只是爲了補充回答以上, 如果你只是想知道的iCloud可用於您的應用程序,例如
1.沒有iCloud帳戶是設置,或
2.文件和數據被禁用(所有應用程序),或
3.文檔和數據是爲你的應用程序只

那麼你可以使用NSFileManager's ubiquityIdentityToken爲殘疾人iOS 6及以上版本
如果值爲零,則未配置iCloud帳戶。否則,配置iCloud帳戶。

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

請注意,根據Apple docs,你可以從主線程調用它。

因爲此方法返回的速度相對較快,所以您可以在啓動時調用它,並且可以從應用程序的主線程調用它。

相關問題