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