Select a contact from your iPhone’s address book and add a new phone number to it.

My scenario is to select a contact from the iPhone address book and display its name and first phone number in the text fields, and meanwhile programmatically add the phone number to your KABOtherLabel.

I use this code to add a contact programmatically;

-(IBAction)addContactToAddressBook:(id)sender { CFErrorRef error = NULL; ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL); ABRecordRef newPerson = ABPersonCreate(); ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFTypeRef)(contactName.text), &error); ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(phoneNumber.text), kABPersonPhoneMobileLabel, NULL); ABMultiValueAddValueAndLabel(multiPhone, @"1-123-456-7890", kABPersonPhoneIPhoneLabel, NULL); ABMultiValueAddValueAndLabel(multiPhone, @"1-987-654-3210", kABOtherLabel, NULL); ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil); CFRelease(multiPhone); ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error); ABAddressBookSave(iPhoneAddressBook, &error); if (error != NULL) { NSLog(@"Some error..."); } } 

This code works just fine.

And I use the following code to search for a contact and display it in text fields;

 -(IBAction)pickContact:(id)sender { ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; picker.peoplePickerDelegate = self; [self presentViewController:picker animated:YES completion:nil]; } //called when user presses the cancel button in the Address book view controller - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [self dismissViewControllerAnimated:YES completion:nil]; } //called when user pics up a contact from the phone address book - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { [self displayPerson:person]; //calls displayPerson:(ABRecordRef)person to show contact information in the app [self dismissViewControllerAnimated:NO completion:NULL]; } - (void)displayPerson:(ABRecordRef)person { NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty); //Extracts the contact first name from address book & assigns it to a string value //NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty); //Extracts the contact last name from address book & assigns it to a string value self.contactName.text = name; NSString* phone = nil; //Extracts the first phone number among multiple contact numbers from address book & assigns it to a string value ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty); if (ABMultiValueGetCount(phoneNumbers) > 0) { phone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0); } else { phone = @"[None]"; } self.phoneNumber.text = phone; } 

This code also works fine, and I get the values ​​in the required text fields.

However, when I combine these two codes, i.e. write the following code at the end of the displayPerson () method or call it from shouldContinueAfterSelectingPerson

The code I used to add details to an existing contact is as follows:

  CFErrorRef error = NULL; ABAddressBookRef myAddressBook = ABAddressBookCreateWithOptions(NULL, NULL); ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multiPhone, @"1-987-654-3210", kABPersonPhoneIPhoneLabel, NULL); ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone,nil); CFRelease(multiPhone); ABAddressBookAddRecord(myAddressBook, person, &error); ABAddressBookSave(myAddressBook, &error); if (error != NULL) { NSLog(@"Some error"); } return NO; 

}

The problem is that only this number is added to the contact, and all other existing entries are deleted. ie this number overwrites all other existing entries. Please, help

+4
source share
1 answer

ABRecordSetValue() sets the record property to the value you give it, overwriting any existing value for the specified property. Since you use ABMultiValueCreateMutable() to create a new empty list of values, you actually ignore any kABPersonPhoneProperty values ​​that may already exist in ABRecord .

Instead, your process for editing an existing entry should be:

  • Call ABRecordCopyValue() on the selected record to get a list of existing values
  • Call ABMultiValueCreateMutableCopy() to get a list of mutable values
  • Add the desired phone number to the list of mutable values ​​using ABMultiValueAddValueAndLabel()
  • Write a list of mutable values ​​with the phone number added to the record using ABRecordSetValue()
+4
source

All Articles