Add phone number to existing contact

2020-07-25 02:22发布

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条回答
够拽才男人
2楼-- · 2020-07-25 03:16

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.

查看更多
登录 后发表回答