NSUbiquityIdentityDidChangeNotification and SIGKILL

Swift 1.2 Xcode 6

For a long time, the listener, the first time the caller.

Hello,

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

Here is the code they provide for implementation:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector (iCloudAccountAvailabilityChanged:) name: NSUbiquityIdentityDidChangeNotification object: nil]; 

I changed it in my application:

 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 this happen in AppDelegate? Do we remove the observer when the application goes to the background?

The problem that I am facing is that when the Ubiquity token changes, the application still terminates because the user has changed the iCloud settings.

How can you all subscribe to this notification, and if you do not, what do you do to track the current iCloud user?

Thanks!

+7
ios xcode swift icloud
source share
4 answers

Short answer

To receive notifications in iOS when a user logs in or out of iCloud when using your application, use CKAccountChangedNotification , not NSUbiquityIdentityChanged .

Long answer

I tried to make it work. I recalled something from one of the talks at WWDC16 that there was something similar that they recommended using. However, from the sample code that they provide, I was able to find NSUbiquityKeyIdentityChanged , which I could not get.

So, I returned to the video (it is from 2015) . What I saw them relates to CKAccountChangedNotification - and works exactly as expected:

  • Run the simulator application from Xcode
  • Exit the settings application on the simulator
  • Sign In (or Sign Out) iCloud Account
  • Return to your application (click the icon on the main screen of the simulator)
    • Notification received.
  • Close the application again.
  • Sign out of your iCloud account (or sign in)
  • Go back to your app again
    • Another notification received.
+9
source share

In Swift 3.0 , another renaming happened:

Now NSUbiquityIdentityDidChangeNotification has changed to NSNotification.Name.NSUbiquityIdentityDidChange .

So, the full registration is as follows:

 // Register for iCloud availability changes NotificationCenter.default.addObserver(self, selector: #selector(...), name: NSNotification.Name.NSUbiquityIdentityDidChange, object: nil) 
+1
source share

I found the same problems, see this question for my comments.

To summarize: on iOS, I think apps will still be killed when the iCloud account changes (or is simply disabled). Therefore, there is no need to listen to NSUbiquityIdentityDidChangeNotification .

If you are on tvOS, your application will not be killed. When your application becomes active, you get one or more NSUbiquitousKeyValueStoreDidChangeExternallyNotification with NSUbiquitousKeyValueStoreChangeReasonKey set to NSUbiquitousKeyValueStoreAccountChange because tvOS swaps your entire value store for value.

Perhaps you could use this with some tricks to detect account changes, for example. save the NSUUID in the material value store, and when the value changes, it means that there is a new account. But you cannot determine if the account is disabled.

0
source share

On iOS 10, I found that NSUbiquityIdentityDidChangeNotification never sent. If I had a CKContainer (as per the docs), the 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 has signed up for an iCloud account or
  • Custom iCloud Drive in iOS 11. This always led to the inclusion of iCloud Drive-> App. However, after obtaining the status of an account, you received NoAccount!
  • Custom iCloud Drive in iOS 10. The subsequent state of the iCloud Drive-> App was what it was when I turned off iCloud Drive. Account status was appropriate. However, if the iCloud Drive-> App was disconnected at that moment, enabling this option did not result in termination or notification.

The application was interrupted if

  • A user has signed out of iCloud regardless of the state of iCloud Drive.
  • User disabled iCloud Drive-> App
  • User has disabled iCloud Drive (even if iCloud Drive-> is already disabled)
  • The user launched the application with iCloud Drive turned on, then turned on iCloud Drive-> App
0
source share

All Articles