Add entry to contacts using iPhone SDK

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.

Advertisements

Tags: , , , , , , , ,

7 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

  3. Sumangala Says:

    Thanks alot for this code… It works out… 🙂

  4. Hudson Says:

    This post is just to add a single-value contact. To add a multi-value contact is very different. Do you how?
    Send me an email.

  5. SAURABH Says:

    Thanx.. It worked gr8.. Can you also help by providing code for saving all details like phone no , email etc .

    Regards
    Saurabh

  6. Sandy Says:

    Thanks,

    It possible to create a contact under a group.. using iPhone SDK?

  7. Ondrej Says:

    Hi,

    Thanks for the great snippet, it showed me the right direction. Anyway, I just finished a full example that allows you to add phone numbers, emails, address, urls and other stuff … so if you want to check it out:

    http://www.xprogress.com/post-56-best-example-of-how-to-add-entry-to-contacts-using-addressbookui-framework-on-iphone-sdk/

    Cheers, great stuff,

    Ondrej 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: