Tuesday, January 5, 2010

iPhone : Update Addressbook contact

Hope you know how to add a contact to AddressBook,
And have stored the recordid in our application while adding.

following is the piece of code to get the contact id in Addressbook
ABAddressBookAddRecord (addressBook, aRecord, &error);
if(ABAddressBookSave ( addressBook, &error)){

recordid = ABRecordGetRecordID (aRecord);

Update a record
inputs are recordid, NSString* firstName,lastName,companyName

ABAddressBookRef addressBook = ABAddressBookCreate();

ABRecordRef aRecord = ABAddressBookGetPersonWithRecordID(addressBook, recordid);

/*** update firstname, lastname, company name **/
CFErrorRef error = NULL;

if([firstName length]==0) firstName = nil;

ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstName, &error);
if([lastName length]==0) lastName = nil;
ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastName, &error);
if(companyName!= nil){

if([companyName length]==0) companyName = nil;
ABRecordSetValue(aRecord, kABPersonOrganizationProperty, companyName, &error);

CFErrorRef error = NULL;
BOOL isSaved = ABAddressBookSave ( addressBook, &error);

No comments:

Post a Comment