首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ABAddressBook - iPhone中向现有ABRecord添加新号码

在ABAddressBook - iPhone中向现有ABRecord添加新号码
EN

Stack Overflow用户
提问于 2011-01-20 02:56:39
回答 1查看 3.4K关注 0票数 4

我正在尝试通过我的应用程序更新地址簿中现有联系人的内容,但不需要UI。场景是这样的:

1用户输入号码和姓名2应用程序检查该姓名是否在联系人列表中3如果是,则它检查该号码是否是该姓名的联系人之一4如果不是,则将其添加到该姓名中

我已经设法完成了步骤1-3,但我找不到方法来做4。有人能帮上忙吗?

下面是我的代码是什么样子的

代码语言:javascript
复制
...
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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-20 18:29:07

在今天早上进一步研究了这些API之后,我设法找到了解决方案。这就是了:

代码语言:javascript
复制
// 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);
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4739417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档