我正在尝试通过我的应用程序更新地址簿中现有联系人的内容,但不需要用户界面。场景是这样的:
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;
}
今天早上进一步查看 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)