objective-c - 将电话号码添加到现有联系人
<p><p>我正在尝试使用 AddressBook 框架向现有联系人添加电话号码,在使用选择器选择一个人后,此方法被调用:</p>
<pre><code>- (BOOL) peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
if(_phoneNumber != nil)
{
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutableCopy (ABRecordCopyValue(person, kABPersonPhoneProperty));
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)_phoneNumber, kABPersonPhoneOtherFAXLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
}
return FALSE;
}
</code></pre>
<p>但在此之后,该号码不会添加到此人的记录中。我做错了什么?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您需要将此记录保存到通讯录中。</p>
<p>使用<code>ABPeoplePickerNavigationController</code>的<code>addressBook</code>属性获取通讯录,然后调用<code>ABAddressBookSave</code>。</p>
<p>这会给你类似的东西:</p>
<pre><code>- (BOOL) peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
if(_phoneNumber != nil)
{
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutableCopy (ABRecordCopyValue(person, kABPersonPhoneProperty));
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)_phoneNumber, kABPersonPhoneOtherFAXLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone,nil);
ABAddressBookRef ab = peoplePicker.addressBook;
CFErrorRef* error = NULL;
ABAddressBookSave(ab, error);
CFRelease(multiPhone);
}
return FALSE;
}
</code></pre>
<p>您可以测试<code>ABAddressBookSave</code>返回值是否成功/失败,并在<code>error</code>变量中获取更多信息。</p></p>
<p style="font-size: 20px;">关于objective-c - 将电话号码添加到现有联系人,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/8834379/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/8834379/
</a>
</p>
页:
[1]