在ABAddressBook - iPhone中为现有ABRecord添加新号码 [英] adding a new number to existing ABRecord in ABAddressBook - iPhone
问题描述
我正在尝试通过我的应用程序更新地址簿中现有联系人的内容,但不需要UI。场景是这样的:
I am trying to update the content of an existing contact in the addressbook through my application but without the need for a UI. The scenario is like this:
1用户输入一个号码和一个名字
2该应用程序检查该名称是否在联系人列表中
3如果是,那么它检查该号码是否是该名称的联系人之一
4如果不是,则将其添加到该名称
1 The user enters a number and a name 2 The application checks if that name is in the contacts list 3 if it is then it checks if the number is one of the contacts for that name 4 If not it adds it to that name
我已设法实现步骤1-3,但我找不到办法4.可以帮助吗?
I have managed to achieve steps 1-3 but i could not find a way to do 4. Can any one help?
下面我的代码是什么样的
Below if what my code looks like
...
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。在这里你去:
After further look this morning at the APIs i managed to find the solution. Here you go:
// 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);
这篇关于在ABAddressBook - iPhone中为现有ABRecord添加新号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!