How to identify iCloud logged in user in airplane mode?

若如初见. 提交于 2019-12-12 02:52:26

问题


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.


回答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.




回答2:


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)
        }
    }


来源:https://stackoverflow.com/questions/25884575/how-to-identify-icloud-logged-in-user-in-airplane-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!