ios - CloudKit: Get users firstname/surname -
i'm trying users first name using cloud kit following code not getting users first name , leaving firstnamefromfunction variable empty. know how achieve in ios 10?
let container = ckcontainer.default() container.fetchuserrecordid { (recordid, error) in if error != nil { print("handle error)") }else{ self.container.discoveruserinfo( withuserrecordid: recordid!, completionhandler: { (userinfo, error) in if error != nil { print("handle error") }else{ if let userinfo = userinfo { print("givenname = \(userinfo.displaycontact?.givenname)") print("familyname = \(userinfo.displaycontact?.familyname)") firstnamefromfunction = userinfo.displaycontact?.givenname }else{ print("no user info") } } }) } }
first need ask user permission discovered.
use ckcontainer.default().requestapplicationpermission
method passing .userdiscoverability on applicationpermission parameter.
the ckcontainer.default().discoveruserinfo
method deprecated on ios 10. instead use ckcontainer.default().discoveruseridentity method.
do like:
ckcontainer.default().requestapplicationpermission(.userdiscoverability) { (status, error) in ckcontainer.default().fetchuserrecordid { (record, error) in ckcontainer.default().discoveruseridentity(withuserrecordid: record!, completionhandler: { (useridentity, error) in print("\(useridentity?.namecomponents?.givenname)") print("\(useridentity?.namecomponents?.familyname)") }) } }
Comments
Post a Comment