Nsubiquityidentitydidchangenotification and Sigkill

NSUbiquityIdentityDidChangeNotification and SIGKILL

Short Answer

To be notified in iOS when a user logs in or out of iCloud while using your app, use CKAccountChangedNotification, not NSUbiquityIdentityChanged.

Long Answer

I've been trying to get this to work as well. I remembered something from one of the talks at WWDC16 that there was something like this that they recommended to use. However, from the sample code they provide, I've only been able to find NSUbiquityKeyIdentityChanged, which I haven't been able to get to work.

So I went back to the video (it's from 2015). That's where I saw them refer to CKAccountChangedNotification – and it works exactly as expected:

  • Launch your app on the simulator from Xcode
  • Exit to the Settings app on the simulator
  • Log in (or out) of iCloud account
  • Go back into your app (tap icon on simulator home screen)

    • A notification is received.
  • Exit to Settings app again
  • Log back out (or in) to iCloud account
  • Go back into your app again

    • Another notification is received.

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

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...
}
}

CKAccountChanged notifications not received

Problem solved:

First, one has to access the container, e.g. by

let container = CKContainer.default()  

This instantiates the container object that can send such notifications.

Second, to get the notification one has to run the app, tap the home button, open systems settings, log in or out to/from the iCloud account, and switch back to the app. Only then is the notification delivered.



Related Topics



Leave a reply



Submit