Saving A User Address Book in iOS Programmatically
I recently built an iOS application that needed to access and save our user’s iOS contacts. It took a little bit of searching around the internet to find a way that worked for me, so I decided to document it here. The first step is to include the AddressBook header file:
This header does not require any linked libraries etc. to compile. The next step is to create an ABAddressBookRef and query the user for permissions (if they have not already given it). The following block of code does this.
If the user has never given the app permisson to it’s contact list, ABAddressBookGetAuthorizationStatus() should return kABAuthorizationStatusNotDetermined. If the user already has access to the contact list, ABAddressBookGetAuthorizationStatus() will return kABAuthorizationStatusAuthorized. After the user has authorized the contact list access, you need to copy them all to an internal data structure (in my case, an array of dictionaries), and then send it to an endpoint, save it to YapDatabase, etc. The following block of code saves all of th contacts first name, last name, and phone number into contactsList.
Finally, if you want to send this data to an endpoint, I recommend you convert the data to JSON before you send it over the wire:
If you have any questions, feel free to reach out to me on twitter and if you need iOS help, please reach me at firstname.lastname@example.org