Get the name of the current CloudKit user

Hi Everyone,


I'm using the latest XCode 8 Beta 2, and I'm unable to get the current iCloud user's name. Here's the code snippit:


CKContainer.default().fetchUserRecordID { (record, error) in
       CKContainer.default().discoverUserIdentity(withUserRecordID: record!, completionHandler: { (userID, error) in
           print(userID?.hasiCloudAccount)
           print(userID?.lookupInfo?.phoneNumber)
           print(userID?.lookupInfo?.emailAddress)
           print((userID?.nameComponents?.givenName)! + " " + (userID?.nameComponents?.familyName)!)
       })
}


record returned in line 1 is a real record. I think this shows that the app is connected to CloudKit and a container correctly.

userID in line 2 is nil as well as error is nil.


Has anyone been able to get the current user's iCloud name in XCode 8 iOS 10 Beta 2?


Wes

Replies

Update:


Looks like you need to ask for permission first:


CKContainer.default().requestApplicationPermission(.userDiscoverability) { (status, error) in
            CKContainer.default().fetchUserRecordID { (record, error) in
                CKContainer.default().discoverUserIdentity(withUserRecordID: record!, completionHandler: { (userID, error) in
                    print(userID?.hasiCloudAccount)
                    print(userID?.lookupInfo?.phoneNumber)
                    print(userID?.lookupInfo?.emailAddress)
                    print((userID?.nameComponents?.givenName)! + " " + (userID?.nameComponents?.familyName)!)
                })
            }
        }

still getting nil value for email why ?

Per the docs lookupInfo is the email or phone used to lookup the identity. Since this is using the CKRecord.ID to do the lookup these are blank.