How to identify iCloud logged in user in airplane

2019-09-17 09:43发布

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.

2条回答
对你真心纯属浪费
2楼-- · 2019-09-17 10:06

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.

查看更多
女痞
3楼-- · 2019-09-17 10:06

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)
        }
    }
查看更多
登录 后发表回答