Reputation: 475
My scenario is to pick a contact from iPhone's address book and display it's name and first phone number into textfields and meanwhile programmatically add a phone number in its KABOtherLabel property.
I am using 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 perfectly.
And Im using following code to pick up a contact and display it in the 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's address book
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self displayPerson:person]; //calls displayPerson:(ABRecordRef)person to show contact's information in the app
[self dismissViewControllerAnimated:NO completion:NULL];
}
- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty); //Extracts the contact's first name from address book & assigns it to a string value
//NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty); //Extracts the contact's 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 perfectly and I get the values in the required text fields.
However when I combine these both codes i-e write the following code at the end of displayPerson() method or call it from shouldContinueAfterSelectingPerson
The code I used to add detail in 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 into the contact and all the other existing entries get removed. i-e this number overwrites all the other existing entries. Please help
Upvotes: 4
Views: 2913
Reputation:
ABRecordSetValue()
sets the property of the record to the value you give it, overwriting any existing value for said property. Because you use ABMultiValueCreateMutable()
to create a new, empty value list, you effectively ignore any values of kABPersonPhoneProperty
that may already exist on the ABRecord
.
Instead, your process for editing an existing record should be:
ABRecordCopyValue()
on a chosen record to get the existing value listABMultiValueCreateMutableCopy()
to get a mutable value listABMultiValueAddValueAndLabel()
ABRecordSetValue()
Upvotes: 4