Add phone number to existing contact

倖福魔咒の 提交于 2019-12-06 11:48:14

问题


I am trying to add a phone number to an existing contact using the AddressBook framework, after selecting a person with the picker this method is called:

- (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;
}

But after this the number is not added to the person's record. What am I doing wrong?


回答1:


You need to save this record to the address book.

Get the address book using the addressBook property of ABPeoplePickerNavigationController, then call ABAddressBookSave.

This gives you something like:

- (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;
}

You can test ABAddressBookSave return value for success / failure, and get additional information in error variable.



来源:https://stackoverflow.com/questions/8834379/add-phone-number-to-existing-contact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!