views:

482

answers:

1

Hi,

In my application I need to implement the address book which should contains the native addressbook details, and the user should be able to add and delete from the address book and it should be updated in the native iphone addressbook.

I read somewhere that the iphone native address book database is accesible. In documentation also I saw that addContact and Delete API's are exposed to addressbook.

Can anyone please tell me how can I access the native AddressBook of the iphone, and.. how to add and delete contacts from the address book? Can anyone post the sample code for this?

+3  A: 

You need to use ABRecords and ABAddressBook. For example, adding can be done:

#import <AddressBook/AddressBook.h>

...

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

ABRecordSetValue(record, kABPersonFirstNameProperty, CFSTR("Kevin"), NULL);
ABRecordSetValue(record, kABPersonLastNameProperty, CFSTR("Sylvestre"), NULL); 

ABAddressBookAddRecord(addressBook, record, NULL);

ABAddressBookSave(addressBook, NULL);

It is important that you add the AddressBook.Framework to your project (right click on 'Frameworks' > 'Add' > 'Existing Frameworks'). The documentation should give you enough to figure out how to remove, etc.

Kevin Sylvestre
this is fine,But my doubt is, suppose in the native contacts application if you press plus(to add contact) if will open one window with the new contact details.How can I invoke this view?and if I press AddressBook button in my application. it should display all the contacts which are there in native iphone contacts database, my problem is accessing the contacts database, how can I do this?
chaitanya
You can access the contacts database using the ABAddressBookCreate(). This creates a copy of the default address book application database.
Kevin Sylvestre