10.4. Inserting a Person Entry into the Address Book

Problem

You want to create a new person contact and insert it into the user’s address book.

Solution

Use the ABPersonCreate function to create a new person. Set the person’s properties using the ABRecordSetValue function and add the person to the address book using the ABAddressBookAddRecord function.

Discussion

After accessing the address book database using the ABAddressBookCreate function, you can start inserting new group and person records into the database. In this recipe, we will concentrate on inserting new person records. For information about inserting new groups into the address book, please refer to Recipe 10.5.

Use the ABPersonCreate function to create a new person record. Bear in mind that calling this function is not enough to add the person record to the address book. You must save the address book for your record to appear in the database.

By calling the ABPersonCreate function, you get a Core Foundation reference to a value of type ABRecordRef. Now you can call the ABRecordSetValue function to set the various properties of a new person entry. Once you are done, you must add the new person record to the database. You can do this using the ABAddressBookAddRecord function. After doing this, you must also save any unsaved changes to the address book database in order to truly preserve your new person record. Do this by using the ABAddressBookSave function.

So let’s combine all this into a method that allows us to insert ...

Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.