0

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?

CKT
  • 1,223
  • 4
  • 21
  • 39
  • 1
    Did you read the Address Book Programming Guide for iPhone OS? – zoul Apr 06 '10 at 06:40
  • Ya I read it, But I am not able use ABAddressBook class to access the addressbook database, If you have any sample code plz post it. – CKT Apr 06 '10 at 06:51

1 Answers1

4

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
  • 37,288
  • 33
  • 152
  • 232
  • 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? – CKT Apr 06 '10 at 06:57
  • You can access the contacts database using the ABAddressBookCreate(). This creates a copy of the default address book application database. – Kevin Sylvestre Apr 06 '10 at 15:23