PDA

View Full Version : how edit contact of Addressbook with API?




jinlianhai
Jan 5, 2009, 09:30 PM
ABAddressBookRef book = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(book);
ABRecordRef ref = CFArrayGetValueAtIndex(people, 1);
ABMultiValueRef mutablePhones =(NSString *)ABRecordCopyValue(ref,kABPersonPhoneProperty);
ABRecordRef current = ABPersonCreate();
NSString *phoneNumber, *phoneNumberLabel;

for (CFIndex i = 0; i < ABMultiValueGetCount(mutablePhones); i++)
{

phoneNumberLabel = (NSString*)ABMultiValueCopyLabelAtIndex(mutablePho nes, i);

// phone number of this record! --old---
phoneNumber = ABMultiValueCopyValueAtIndex(mutablePhones, i);


if ( YES == [ phoneNumberLabel isEqualToString: @"_$!<Mobile>!$_"] )
{

NSString *mainPhone = @"111111111111111111111";

if(ABMultiValueReplaceValueAtIndex (mutablePhones, mainPhone,i))
{
ABRecordSetValue(current, kABPersonPhoneProperty, mutablePhones, NULL);

// phone number of this record! --new---
NSString *phoneNumber2 = ABMultiValueCopyValueAtIndex(mutablePhones, i);

NSLog(@"that phone number has been updated to %@.", mainPhone);
}
}
}

ABAddressBookSave(book, nil);


contact's mobile phone number was not be changed! why?



rantanplan41
Aug 31, 2009, 03:31 PM
Hi,

you need to do a mutableCopy before.

ABAddressBookRef book = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(book);
ABRecordRef ref = CFArrayGetValueAtIndex(people, 1);
ABMultiValueRef mutablePhones =(NSString *)ABMultiValueCreateMutableCopy(ABRecordCopyValue(ref,kABPersonPhoneProperty));
ABRecordRef current = ABPersonCreate();
NSString *phoneNumber, *phoneNumberLabel;