gpt4 book ai didi

iphone - 将新号码添加到 ABAddressBook 中的现有 ABRecord - iPhone

转载 作者:行者123 更新时间:2023-12-03 19:09:37 25 4
gpt4 key购买 nike

我正在尝试通过我的应用程序更新地址簿中现有联系人的内容,但不需要 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;
}

最佳答案

今天早上进一步查看 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);

关于iphone - 将新号码添加到 ABAddressBook 中的现有 ABRecord - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4739417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com