我正在尝试通过我的应用程序更新地址簿中现有联系人的内容,但不需要UI。场景是这样的:
1用户输入号码和姓名2应用程序检查该姓名是否在联系人列表中3如果是,则它检查该号码是否是该姓名的联系人之一4如果不是,则将其添加到该姓名中
我已经设法完成了步骤1-3,但我找不到方法来做4。有人能帮上忙吗?
下面是我的代码是什么样子的
...
CFIndex lTotalContactsCount = ABAddressBookGetPersonCount(lAddressBook);
NSArray *people = (NSArray *)ABAddressBookCopyArrayOfAllPeople(lAddressBook );
for (CFIndex i = 0; i < lTotalContactsCount; i++)
{
ABRecordRef lRef = (ABRecordRef)[people objectAtIndex:i];
...
// if names match
{
ABMutableMultiValueRef lPhoneNumbers = ABRecordCopyValue(lRef, kABPersonPhoneProperty);
CFIndex lContactPhoneNumberCount = ABMultiValueGetCount(lPhoneNumbers);
ABRecordID contactID = ABRecordGetRecordID(lRef);
...
// if numbers dont match
{
// THIS BIT IS NOT WOKRING
CFErrorRef error = NULL;
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, number, (CFStringRef)@"Duplicate", NULL);
// ABRecordSetValue(newPerson, kABPersonFirstNameProperty, name, &error);
//add the number to the contact
ABRecordSetValue(lRef, kABPersonPhoneProperty, multiPhone,nil);
// ABAddressBookAddRecord(lAddressBook, lRef, &error);
ABAddressBookSave(lAddressBook, &error);
}
if( firstName )
CFRelease(firstName);
if( lastName )
CFRelease(lastName);
if( lPhoneNumbers )
CFRelease(lPhoneNumbers);
// no need to search other entries
if(numberExists)
break;
}发布于 2011-01-20 18:29:07
在今天早上进一步研究了这些API之后,我设法找到了解决方案。这就是了:
// contactId is the ID of the person i need to add a new number to his contacts
// got the id through : ABRecordGetRecordID( ABRecordRef )
ABRecordRef person = ABAddressBookGetPersonWithRecordID(lAddressBook, contactID);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutableCopy(lPhoneNumbers);
ABMultiValueAddValueAndLabel(multiPhone, number, (CFStringRef)@"Duplicate", NULL);
//add the number to the contact
ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone,nil);
ABAddressBookSave(lAddressBook, &error);https://stackoverflow.com/questions/4739417
复制相似问题