Add entry to contacts using iPhone SDK

By Olav Rask

So i recently started programming for iPhone. As part of the project i’m currently working on, i have to add new entries to the phones address book (contacts). The address book api is not very intuitive, seems to be a straight C api and is poorly documented. It took me a lot of googling to find the solution, so i promissed my self i would post the code once i found it .

I finally found what i was looking for over at the macrumors.com forum (http://forums.macrumors.com/showthread.php?t=449981&page=2 – post nr. 35 by MediadorAv - code below is a straight copy, with english naming).

This is what the code looks like:

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();


ABRecordSetValue(person, kABPersonFirstNameProperty, @"Kate" , nil);
ABRecordSetValue(person, kABPersonLastNameProperty, @"Hutson", nil);
ABAddressBookAddRecord(addressBook, person, nil);
ABAddressBookSave(addressBook, nil);

CFRelease(person);

Code should be pretty self explanatory. To get it running, you need to add the AddressBook framework to your project, and import AddressBook/ABAddressBook.h and AddressBook/ABPerson.h. For more property constants, have a look at ABPerson.h.

Tags: , , , , , , , ,

2 Responses to “Add entry to contacts using iPhone SDK”

  1. Brainwash Inc. » Blog Archive » iPhone: add contacts to the address book Says:

    [...] From: Add entry to contacts using iPhone SDK « Mr. Rask [...]

  2. James Sun Says:

    Olav -

    The sample code you posted worked great! Thanks!

    James

Leave a Reply