Find User Name or First/Last Name in iOS9 CloudKit / ABPerson (ABAddressBook)

3k views Asked by At

iOS9 deprecates user info features for CloudKit and ABPerson.

e.g. in the properties of CKDiscoveredUserInfo

firstName" Deprecated in iOS 9.0

lastName" Deprecated in iOS 9.0

I want last name and first name as previously provided in iOS8 CloudKit.

How to retrieve the similar information in iOS9 CloudKit, ABPerson or something else?

Short of grace method is this

[[UIDevice currentDevice] name]; 

 NSArray *nameArray = [[NSHost currentHost] names];
 NSString *user = [nameArray objectAtIndex:0];

Both print e.g. John'iPhone.

2

There are 2 answers

1
Edwin Vermeer On

In the documentation for CKDiscoverAllContactsOperation you can see this:

The search of the user’s address book does not return any personal data about the user’s contacts. The search returns only the IDs of the corresponding user records, which contain only data that your app puts there. CloudKit uses the address book information to identify users of the app that the current user knows. Because the system accesses the address book instead of your app, the system does not display a prompt to the user when that access occurs.

When looking in the CloudKit dashboard, you can see that there is also no name in the User table. So you can also not fetch it.

I assume Apple is making the service anonymous. You have to implement something yourself to attach a name to an id. As you described you can use the device name or you can let the user itself enter his name.

My first idea was to implement this by first querying all contacts using ABAddressBookCopyArrayOfAllPeople and then looking up all emails using CKDiscoverUserInfosOperation for each email. but apparently that one was also discontinued in iOS 9.

It looks like Apple is stopping access to all personal data. when you go to the prerelease library and filter for address you will still see a lot of references. But as soon as you go to a detail page, you will only see everything is deprecated.

0
Kevin On

For iOS9, obtaining user personal contact information via Contact Framework

The Contacts framework provides an Objective-C and Swift API to access the user’s contact information.

Replacing ABAdressBook

This framework is available on all Apple platforms and replaces the Address Book framework in iOS and OS X.

Specifically, CNContact

The CNContact is a thread-safe class that represents an immutable value object for contact properties, such as the first name and phone numbers of a contact.

Fetching Contacts

You can fetch contacts using the contact store (CNContactStore), which represents the user's contacts database

Searching for User's Contacts database (New in iOS 9.0)

e.g.given name, family name, birthday, phone number and more.

let store = CNContactStore()
let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName("Appleseed"), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey])

let contact = contacts!.first  //assuming contain at least one contact

// Checking if phone number is available for the given contact.
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
    print("\(contact.givenName) \(contact.familyName)")
} else {
    //Refetch the keys
    let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey]
    let refetchedContact = try store.unifiedContactWithIdentifier(contact.identifier, keysToFetch: keysToFetch)
    print("\(refetchedContact.givenName) \(refetchedContact.familyName)")
}

Searching for Current User Info

if let personInfo:CNContact = CKDiscoveredUserInfo.displayContact{

println("first name: \(personInfo.givenName) last name: \(personInfo.familyName) ")   }