0

I try to get userRecordID in airplane mode, but I get an error, any other way?

class func asdf() {

    var defaultContainer = CKContainer.defaultContainer()
    var publicDatabase = defaultContainer.publicCloudDatabase

    defaultContainer.fetchUserRecordIDWithCompletionHandler({ userRecordID, error in

        if error == nil {

            println("userRecordID.recordName : \(userRecordID.recordName)")
        } else {
            println("\(error.localizedDescription)")
        }
    })
}

Terminal: Couldn't renew our secure session

I put an accountStatusWithCompletionHandler call outside of fetchUserRecordIDWithCompletionHandler, that returned CKAccountStatus.Available.

János
  • 32,867
  • 38
  • 193
  • 353

2 Answers2

1

You cannot detect internet connectivity with CloudKit. It will only give you an error when there is no connectivity. If you do want to test for internet connectivity, then you could use the famous Reachability class like this: How to check for an active Internet connection on iOS or OSX?

If you want to detect changes to the iCloud account, then you can add the following code to your AppDelegate application didFinishLaunchingWithOptions:

var localeChangeObserver = NSNotificationCenter.defaultCenter().addObserverForName(NSUbiquityIdentityDidChangeNotification, object: nil, queue: NSOperationQueue.mainQueue()) { _ in
    println("The user’s iCloud login changed: should refresh all user data.")
}

If you then want to fetch the user id, you have to do a container.requestApplicationPermission to see if you are allowed to query an then a container.fetchUserRecordIDWithCompletionHandler. Bit this requires internet connection. You could cache it on the device together with the detection code above to get the correct status.

Community
  • 1
  • 1
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • no no, it is not internet connectivity what I am curious about, I want to know who is the user, at least is this user the same who used app last time -even if it is now in airplane mode-, so at least an anonymous identifier I need, no family, first name, only recordID – János Sep 17 '14 at 08:35
  • do you know, do I get `NSUbiquityIdentityDidChangeNotification` if my app is not running? btw. I have set up in Capabilities `Background Modes`: `Remote Notifications` – János Sep 17 '14 at 08:51
  • The moment you logout to iCloud in the settings app, you will see the log message in your debugger. So it looks like the notification will work in background mode. – Edwin Vermeer Sep 17 '14 at 08:54
0

I came across to this code, comparing recently and previous logged in user's token, and if the same, use the previously downloaded userRecordID. The only problem that in some cases on my iPad ubiquityIdentityToken method returns nil even dow I am logged in, strange.

class func checkUser() {

        let ubiquityIdentityToken = NSFileManager.defaultManager().ubiquityIdentityToken
        let status = Utility.status()
        let prevUbiquityIdentityToken = status.objectForKey("ubiquityIdentityToken")

        if ubiquityIdentityToken != nil && ubiquityIdentityToken!.isEqual(prevUbiquityIdentityToken) {

        } else if ubiquityIdentityToken != nil && !ubiquityIdentityToken!.isEqual(prevUbiquityIdentityToken) {

            status.setObject(ubiquityIdentityToken!, forKey: "ubiquityIdentityToken")
            Utility.saveStatus(status)

            let defaultContainer = CKContainer.defaultContainer()
            let publicDatabase = defaultContainer.publicCloudDatabase

            defaultContainer.fetchUserRecordIDWithCompletionHandler({ userRecordID, error in
                if error == nil {
                    //do some stuff
                    }) 
                } else {
                    println("\(error.localizedDescription)")
                }
            })
        } else {
            //do some stuff
            status.removeObjectForKey("ubiquityIdentityToken")
            Utility.saveStatus(status)
        }
    }
János
  • 32,867
  • 38
  • 193
  • 353