Description
Code could be added to detect changes to the user's iCloud account status so the connectStatus property value is automatically updated and (possibly) severed connections can be automatically restored.
I would like your opinion on adding this to your library. It makes sense to me, since it seems your library is meant to avoid the need to directly work with CloudKit or manage error conditions related to the user's iCloud account status. Basically, some code like I have in my tvOS app would need to be added:
// NotificationManager class from http://moreindirection.blogspot.com/2014/08/nsnotificationcenter-swift-and-blocks.html. Could modify to use NSNotificationManager directly in this case.
private let notificationManager = NotificationManager()
notificationManager.registerObserver(NSUbiquityIdentityDidChangeNotification) { [unowned self] note in
// Verify iCloud account status hasn't changed
self.verifyiCloudAccountStatus()
}
func verifyiCloudAccountStatus() {
Async.main { [unowned self] in
var reset: Bool = false
let tokenKey = "NL.EVICT.CloudKit.UbiquityIdentityToken"
// Get the current iCloud token
let currentToken = NSFileManager.defaultManager().ubiquityIdentityToken
if currentToken != nil {
// Check for a previous token being written to user defaults and compare tokens if found
let existingToken = NSUserDefaults.standardUserDefaults().objectForKey(tokenKey)
if (existingToken == nil || currentToken!.isEqual(existingToken)) {
let newTokenData: NSData = NSKeyedArchiver.archivedDataWithRootObject(currentToken!)
NSUserDefaults.standardUserDefaults().setObject(newTokenData, forKey: tokenKey)
reset = true
}
} else {
NSUserDefaults.standardUserDefaults().removeObjectForKey(tokenKey)
reset = true
}
if reset {
// TODO: Update connectStatus property and possibly reestablish connections as needed
}
}
}
Also, FYI, this will probably be the last change I will be suggesting for a while. Your library works great overall. The effort you've put into it is greatly appreciated. I don't know of anything else I need from it that it doesn't already deliver at this point.