How to detect if the user has iCloud enabled for m

2019-06-19 00:15发布

I have developed an iCloud enabled iPhone App, but the issue I m facing is that even if user turn off iCloud backup for my App then also it takes back up on iCloud and reflect changes on my other devices, so I want to know that how can I get that my App is iCloud enabled or not.

It's specifically for App not for Device, if device is iCloud enabled but my App is turned off then it should not take any back ups.

2条回答
小情绪 Triste *
2楼-- · 2019-06-19 00:48

Please use this to check the status of your iCloud whether it is enabled or not .

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSURL *iCloudURL = [fileManager URLForUbiquityContainerIdentifier:nil]; 
NSLog(@"%@", [iCloudURL absoluteString]); 

if(iCloudURL){ 
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore]; 
[iCloudStore setString:@"Success" forKey:@"iCloudStatus"]; 
[iCloudStore synchronize]; // For Synchronizing with iCloud Server 
NSLog(@"iCloud status : %@", [iCloudStore stringForKey:@"iCloudStatus"]); 
}

This will tell you whether it is ON/OFF :)

查看更多
霸刀☆藐视天下
3楼-- · 2019-06-19 00:54

I recommend using this instead if you are using key-value and not document storage.

id iCloudToken = [[NSFileManager defaultManager] ubiquityIdentityToken];
if ( iCloudToken != nil) {
// User has enabled icloud in your app...
} else {
// Icloud is not enabled for your app...
}

Now the reason i recommend the method above is that in the new Xcode compiler you can now dictate if you are going to use document storage or not. If you choose to only use key-value storage, the NSFileManager check will fail as you will not have the container identifier defined in the entitlements. However, ubiquityIdentityToken will always exists if the user has allowed your app to run meaning it will always fire true or false if the user has allowed your app access to iCloud.

查看更多
登录 后发表回答