我试图通过我的应用程序更新地址簿中现有联系人的内容,但不需要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;
}
Run Code Online (Sandbox Code Playgroud)
今天早上进一步了解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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3350 次 |
| 最近记录: |