简体   繁体   中英

Get iCloud name and email swift3

I am using this code to try to get a users iCloud info:

CKContainer.default().requestApplicationPermission(.userDiscoverability) { (status, error) in
        self.container.fetchUserRecordID { (recordIDFetched, error) in
            if error == nil {
                if let temp = recordIDFetched {
                    let temp2 = CKUserIdentityLookupInfo(userRecordID: temp)
                    let fetchedID = CKDiscoverUserIdentitiesOperation(userIdentityLookupInfos: [temp2])
                    print("\nIDCount")
                    print(fetchedID.userIdentityLookupInfos.count)
                    print("\nIDInfo")
                    print(fetchedID.userIdentityLookupInfos[0].userRecordID ?? "Default RecordID")
                    print("\nIDEmail")
                    print(fetchedID.userIdentityLookupInfos[0].emailAddress ?? "Default Email")
                    print("\nIDPhone")
                    print(fetchedID.userIdentityLookupInfos[0].phoneNumber ?? "Default Phone")
                }
            } else {
                print("Error")
                print(error ?? "Default Error")
            }

        }
    }

However when I run it it returns:

IDCount

1

IDInfo

CKRecordID: 0x6080002303a0; recordName=_06a68abd6423ca06532871a8803f31ee, zoneID=_defaultZone: defaultOwner

IDEmail

Default Email

IDPhone

Default Phone

What am I doing wrong? I know the user is logged into iCloud as it is fetching some data from the server ok - its just this bit that fails.

I can't test my solution right now, but it seems that you need a couple of extra steps.

You shoud invoke the user identity operation block from the CKDiscoverUserIdentitiesOperation class

As CKDiscoverUserIdentitiesOperation inherits from CKOperation I think that you should use the add method to add the operation to the CKContainer in other to execute it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM