11

How to detect if iCloud account being used on a device changed?

A user signs out from Settings > iCloud and another user signs in his/her account.

How to detect this change when the app is opened?

erkanyildiz
  • 13,044
  • 6
  • 50
  • 73

1 Answers1

19

Just add an observer for the notification with name NSUbiquityIdentityDidChangeNotification

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

If a user signs out of iCloud, such as by turning off Documents & Data in Settings, the ubiquityIdentityToken method returns nil. To enable your app to detect when a user signs out and signs back in, register for changes in iCloud account availability. - Apple Documentation

http://developer.apple.com/library/mac/#documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html

Ivan Lisovyi
  • 538
  • 5
  • 13
  • I just can't believe that I couldn't find ubiquityIdentityToken in ref docs. Thank you very much. – erkanyildiz Feb 22 '13 at 10:51
  • 7
    The user could also log out or back in while your app is not running. You should save the value of the ubiquity token somewhere and, when your app launches, compare the current value to the previously saved value. – Tom Harrington Feb 22 '13 at 18:02