Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUbiquityIdentityDidChangeNotification and SIGKILL

Swift 1.2 Xcode 6

Long-time listener, first-time caller.

Hello,

Straight from the horse's mouth: "To handle changes in iCloud availability, register to receive the NSUbiquityIdentityDidChangeNotification notification."

Here is the code they provide to implement this:

[[NSNotificationCenter defaultCenter]

addObserver: self

   selector: @selector (iCloudAccountAvailabilityChanged:)

       name: NSUbiquityIdentityDidChangeNotification

     object: nil];

I Swiftified it in my app to:

var observer = NSNotificationCenter.defaultCenter().addObserverForName
(NSUbiquityIdentityDidChangeNotification, object: nil, queue: NSOperationQueue.mainQueue()
){...completion block...}

src: https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html#//apple_ref/doc/uid/TP40012094-CH6-SW6

What is the correct way to implement this? Does it go in the AppDelegate? Do we remove the observer when the app gets sent to the background?

The problem I'm encountering is that when the Ubiquity Token changes, the app is terminated anyway because the user has changed iCloud settings.

How do you all manage to subscribe to this notification, and, if you don't, what do you do instead to keep track of the current logged in iCloud user?

Thank you!

like image 976
Kevinontheweb Avatar asked Apr 09 '15 18:04

Kevinontheweb


3 Answers

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.
like image 80
Anthony C Avatar answered Oct 20 '22 04:10

Anthony C


In Swift 3.0 there was another renaming:

Now the NSUbiquityIdentityDidChangeNotification has changed into NSNotification.Name.NSUbiquityIdentityDidChange.

So the full registering is the following:

// Register for iCloud availability changes
NotificationCenter.default.addObserver(self, selector: #selector(...), name: NSNotification.Name.NSUbiquityIdentityDidChange, object: nil)
like image 22
LukeSideWalker Avatar answered Oct 20 '22 04:10

LukeSideWalker


On iOS 10 I found that NSUbiquityIdentityDidChangeNotification was never sent. Provided I had a CKContainer (as per the docs), CKAccountChangedNotification was sent in very limited circumstances.

Built with xCode 9.1 then tested on iOS 10.02 iPhone 6+ and iOS 11.0.3 iPhone SE

CKAccountChangedNotification was sent if

  • User logged into iCloud account, or
  • User enabled iCloud Drive in iOS 11. This always resulted in iCloud Drive->App being enabled. However, fetching the account status afterwards yielded NoAccount!
  • User enabled iCloud Drive in iOS 10. The subsequent state of iCloud Drive->App was whatever it was when I disabled iCloud Drive. The account status was appropriate. However, if iCloud Drive->App was disabled at this point, enabling it did not produce a termination or a notification.

Application was terminated if

  • User logged out of iCloud regardless of iCloud Drive status
  • User disabled iCloud Drive->App
  • User disabled iCloud Drive (even if iCloud Drive->App already disabled)
  • User started the app with iCloud Drive enabled, then enabled iCloud Drive->App
like image 1
Airsource Ltd Avatar answered Oct 20 '22 04:10

Airsource Ltd