Thursday 11 December 2014

Working with ABAddressBook in Swift

ABAddressBook By definition :
The ABAddressBook opaque type (whose instances are known as address books) provides a programming interface to the Address Book—a centralized database used by multiple applications to store personal information about people.


Main points covered :

  • Fetch contacts from AddressBook
  • Insert/Edit new contact to AddressBook
  • Delete contact from AddressBook
  • Register call back to listen changes in AddressBook to our App

Some common steps for all actions:

  • Import  AddressBook
    import AddressBook
  • Get addressBook reference

    let addressBook : ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()

  • Get user permission to access AddressBook
    ABAddressBookRequestAccessWithCompletion(addressBook, { (granted : Bool, error: CFError!) -> Void in           if granted == true
         {
               //do stuff here
         }
    })

    once user grants permission to access addressBook, we can move further.
  • Fetch contacts from AddressBook
    let allContacts : NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
    for contactRef:ABRecordRef in allContacts { // first name if let firstName = ABRecordCopyValue(contactRef, kABPersonFirstNameProperty).takeUnretainedValue() as? NSString { //Use firstName }

    // Similarly all properties with same kind of ABPropertyID can be fetched. i.e kABPersonLastNameProperty, kABPersonMiddleNameProperty etc }

  • Insert / Edit new contact to AddressBook

    - Create new contact reference
    contactRef = ABPersonCreate()?.takeUnretainedValue() as ABRecordRef!


    - Or get contact reference of contact to be edited
    contactRef = ABAddressBookGetPersonWithRecordID(addressBookRef!, CONTACT_UNIQUE_ADDRESSBOOK_ID).takeUnretainedValue() as ABRecordRef!

    - Set properties

    //First Name
    ABRecordSetValue(contactRef, kABPersonFirstNameProperty, "Test FirstName" nil)

    //Last Name
    ABRecordSetValue(contactRef,  kABPersonLastNameProperty, "Test LastName" nil)


    so on for all required properties.

    - Add contact reference to address book and save it
    ABAddressBookAddRecord(addressBookRef!, contactRef, nil); let success = ABAddressBookSave(addressBookRef!, nil);


    // success will be true for successful addition
Delete contact from AddressBook

- Get contact reference of contact to be deleted
contactRef = ABAddressBookGetPersonWithRecordID(addressBookRef!, CONTACT_UNIQUE_ADDRESSBOOK_ID).takeUnretainedValue() as ABRecordRef!


- Delete contact from addressbook and save it

ABAddressBookRemoveRecord(addressBook!, contactRef!, &errorRef)
ABAddressBookSave(addressBook!, &errorRef)
  • Register call back to listen changes in AddressBook to our App

book = ABAddressBookCreateWithOptions(NULL, NULL); ABAddressBookRegisterExternalChangeCallback(book, addressBookChanged, nil);

and,
void addressBookChanged(ABAddressBookRef reference, CFDictionaryRef dictionary, void *context) {
   // handle call back here..
}