Contacts Framework
Authorizing Contact Access
Section titled “Authorizing Contact Access”Importing the framework
Section titled “Importing the framework”import ContactsObjective-C
Section titled “Objective-C”#import <Contacts/Contacts.h>Checking accessibility
Section titled “Checking accessibility”switch CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts){case .Authorized: //access contactscase .Denied, .NotDetermined: //request permissiondefault: break}Objective-C
Section titled “Objective-C”switch ([CNContactStore authorizationStatusForEntityType:CNEntityType.Contacts]){case CNAuthorizationStatus.Authorized: //access contacts break;case CNAuthorizationStatus.Denied: //request permission break;case CNAuthorizationStatus.NotDetermined: //request permission break;}Requesting Permission
Section titled “Requesting Permission”var contactStore = CKContactStore()contactStore.requestAccessForEntityType(CKEntityType.Contacts, completionHandler: { (ok, _) -> Void in if access{ //access contacts }}Accessing Contacts
Section titled “Accessing Contacts”Applying a filter
Section titled “Applying a filter”To access contacts, we should apply a filter of type NSPredicate to our contactStore variable which we defined it in Authorizing Contact Access example. For example, here we want to sort out contacts with name matching with our own:
let predicate = CNContact.predicateForContactsMatchingName("Some Name")Objective-C
Section titled “Objective-C”NSPredicate *predicate = [CNContact predicateForContactsMatchingName:@"Some Name"];Specifying keys to fetch
Section titled “Specifying keys to fetch”Here, we want to fetch the contact’s first name, last name and profile image:
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactImageDataKey]Fetching contacts
Section titled “Fetching contacts”do { let contacts = try contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)} catch let error as NSError { //...}Accessing contact details
Section titled “Accessing contact details”print(contacts[0].givenName)print(contacts[1].familyName)let image = contacts[2].imageDataAdding a Contact
Section titled “Adding a Contact”import Contacts
// Creating a mutable object to add to the contactlet contact = CNMutableContact()
contact.imageData = NSData() // The profile picture as a NSData object
contact.givenName = "John"contact.familyName = "Appleseed"
let homeEmail = CNLabeledValue(label:CNLabelHome, value:"john@example.com")let workEmail = CNLabeledValue(label:CNLabelWork, value:"j.appleseed@icloud.com")contact.emailAddresses = [homeEmail, workEmail]
contact.phoneNumbers = [CNLabeledValue( label:CNLabelPhoneNumberiPhone, value:CNPhoneNumber(stringValue:"(408) 555-0126"))]
let homeAddress = CNMutablePostalAddress()homeAddress.street = "1 Infinite Loop"homeAddress.city = "Cupertino"homeAddress.state = "CA"homeAddress.postalCode = "95014"contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]
let birthday = NSDateComponents()birthday.day = 1birthday.month = 4birthday.year = 1988 // You can omit the year value for a yearless birthdaycontact.birthday = birthday
// Saving the newly created contactlet store = CNContactStore()let saveRequest = CNSaveRequest()saveRequest.addContact(contact, toContainerWithIdentifier:nil)try! store.executeSaveRequest(saveRequest)