iosobjective-ccore-dataiclouduimanageddocument

Is it possible to intercept iCloud switching on/off in Settings -> iCloud -> Document & Data?


Is it possible to intercept if the user switches from on to off the iCloud support under Settings -> iCloud -> Document & Data?

Obviously when he does this the app has already resigned active and entered the background. I am targeting iOS7 and I would like to keep in sync the UIManagedDocument, otherwise it would be like having two different UIDocuments: one with iCloud support and all the data created until the switch from on to off and a new one without any data in it. If I create data when iCloud support has been switched to off, and then I switch back to on I get the same DB I had when the support has been switched to off.

Note: I believe nelico's answer is right. He wrote: "If your app is running and the user changes enables or disable Document & Data iCloud syncing via the settings app, your app will receive the SIGKILL signal."

When the user changes the settings the app is ALREADY in the background and receives the SIGKILL signal. This is what I do not understand and I do not want. Registering for NSUbiquityIdentityDidChangeNotification doesn't solve this problem.


Solution

  • Another, cleaner, solution is to listen for the NSUbiquityIdentityDidChangeNotification notification and when you get that notification then check the URLForUbiquityContainerIdentifier if it is null they either signed out or turned off 'Documents and Data'. You should also be tracking the current ubiquity token so that you can know not only if they logged off but if they changed iCloud accounts. It happens more than one might think because Apple Geniuses like to just create a new iCloud account when things go wrong on users devices.

    ex:

    id <NSObject,NSCopying,NSCoding> _currentUbiquityIdentityToken;
    

    ...

    _currentUbiquityIdentityToken = [[NSFileManager defaultManager] ubiquityIdentityToken];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector (_iCloudAccountAvailabilityChanged:) name: NSUbiquityIdentityDidChangeNotification object: nil];
    

    ...

    - (void)_iCloudAccountAvailabilityChanged:(NSNotification*)notif {
        if (![_currentUbiquityIdentityToken isEqual:[[NSFileManager defaultManager] ubiquityIdentityToken]]) {
            // Update the current token and rescan for documents.
            _currentUbiquityIdentityToken = [[NSFileManager defaultManager] ubiquityIdentityToken];
            // Do something about the change here...
        }
    }