1

How to I can edit first name and last on my contact from ABAddressBook.

I used this code to find some contacts with name.

+(CFArrayRef)searchContactOnDevice_fromFullName:(NSString *)FullName{
    NSString *searchName = [NSString stringWithFormat:@"%@", FullName];
    ABAddressBookRef addressbook = ABAddressBookCreate();
    CFStringRef nameRef = (__bridge CFStringRef) searchName;
    CFArrayRef  allSearchRecords = ABAddressBookCopyPeopleWithName(addressbook, nameRef);
    return allSearchRecords;
}

If I want to remove some contacts, I can use this code:

+(void)removeContactWithRecordsList:(CFArrayRef) selectedRecords_
{
    ABAddressBookRef addressbook = ABAddressBookCreate();
    if (selectedRecords_ != NULL)
    {
        int count = CFArrayGetCount(selectedRecords_);
        for (int i = 0; i < count; ++i)
        {
            ABRecordRef contact = CFArrayGetValueAtIndex(selectedRecords_, i);
            ABAddressBookRemoveRecord(addressbook, contact, nil);
        }
    }
    ABAddressBookSave(addressbook, nil);
    CFRelease(addressbook);
}

But, I need to edit firstName and lastName for contacts.

How to I can make it.

1 Answers1

0

This code not tested on Xcode....Its an overall idea which should work a/c to me cause I have done this long time ago...Try this...

//Code to edit contact programmatically...     
 ABAddressBookRef addressbook = ABAddressBookCreate();
    if (selectedRecordsCount_ != NULL)
    {           
       ABRecordRef contact = CFArrayGetValueAtIndex(selectedRecordsCount_, index);
       contact.firstName = @"My new first name";
       contact.lastName= @"My New last name":
        ABAddressBookSave(addressbook, nil);
    }

    CFRelease(addressbook);

Here the whole idea is to fetch a ABRecordRef object (person object) and modify the same... then save the addressbook ....which will save you contact's edited information.

Please let me know if you have anything more to help with this issue

Prasanna
  • 945
  • 10
  • 24