Marco Boschi
Marco Boschi

Reputation: 2333

Detect iCloud availability change on OS X

I have a pair of app, one for iOS and one for OS X, that share data through CloudKit private database and iCloud key-value store if the user chooses to, he's logged in and iCloud Data & Documents is enabled for my app and it all works fine with iCloud enabled or disabled from the beginning.

On the iOS side I can detect user choice in Settings app using ubiquityIdentityToken and URLForUbiquityContainerIdentifier("iCloud.my.default.container.for.osx") from NSFileManager to detect if user is logged in and if he enabled Data & Documents and they are nil when expected: when they are nor CloudKit nor Key-Value store works (as expected).

When changing these two settings while the app is running it is terminated so I can check for changes when it's opened again, so no problem. When I tested the same code on OS X I found that if I turn off Data & Documents in System Preferences the app is not killed but I guess it's the intended behavior, but even if I listen for iCloud availability change using

NSNotificationCenter.defaultCenter().addObserver(self, selector: "checkiCloud:", name: NSUbiquityIdentityDidChangeNotification, object: nil)

my app is not notified. If I manually check iCloud availability, i.e. by restarting the app, I found that ubiquityIdentityToken is not nil and that's correct as I am logged in iCloud, but even URLForUbiquityContainerIdentifier("iCloud.my.default.container.for.osx") is not nil and that's not what I expect.

Is this the expected behavior on OS X? If so, how can I check if iCloud is disabled and get notified when something changes? Or is this a bug (I'm testing on El Capitan)?

UPDATE: Disabling iCloud Drive all together make the notification fire and URLForUbiquityContainerIdentifier() return nil so I guess it's a bug.

UPDATE: I've filed a BugReport to Apple: #22973458

Upvotes: 0

Views: 357

Answers (1)

coolcool1994
coolcool1994

Reputation: 3804

If you are building Document-based OS X app, you can use

if ([[NSFileManager defaultManager] ubiquityIdentityToken])
//iCloud Drive is enabled

to check whether or not iCloud Drive is enabled for the device.

Upvotes: 1

Related Questions