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