How to programmatically detect if the iCloud is enabled on user's device when only use NSUbiquitousKeyValueStore?

狂风中的少年 提交于 2019-11-29 12:06:12

问题


I am using NSUbiquitousKeyValueStore to sync some preference data to iCloud. I found that if the user disable "Document & Data" item of iCloud in "Setting App", NSUbiquitousKeyValueStore can not synchronize its data to iCloud. So, I want to first check if this setting is switched on. I found this Code snippet:

    NSURL *ubiq = [[NSFileManager defaultManager] 
                   URLForUbiquityContainerIdentifier:nil];
    NSLog(@"url=%@",ubiq);
    if (!ubiq) {
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:nil message:@"Please enable iCloud in Setting app" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [av show];
        return;

    }

What I want to know is whether this is the only way to detect, even if I just use NSUbiquitousKeyValueStore (not iCloud document storage). Is there a better alternative?


回答1:


Basically, NSUbiquitousKeyValueStore is always available, even if the user does not have an iCloud account (in this case, NSUbiquitousKeyValueStore is just a simple local storage). So in general, you don't need to ask yourself or the user if there is a configured iCloud account.

If you really want to know for sure, in iOS 5, the only solution is the one you're mentioning.




回答2:


I don't know if the docs have been updated, but Apple is now saying:

To determine if iCloud is available, especially at launch time, check the value of the ubiquityIdentityToken property instead

See NSFileManager for both ubiquityIdentityToken and URLForUbiquityContainerIdentifier.

Note also that Apple is saying that URLForUbiquityContainerIdentifier can be relatively slow, and that checking ubiquityIdentityToken is relatively fast.

This is for iOS6 and higher.



来源:https://stackoverflow.com/questions/11006610/how-to-programmatically-detect-if-the-icloud-is-enabled-on-users-device-when-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!