简体   繁体   中英

pick a contact from iPhone's address book and add a new phone number into it

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 ie 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. ie this number overwrites all the other existing entries. Please help

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:

  • Call ABRecordCopyValue() on a chosen record to get the existing value list
  • Call ABMultiValueCreateMutableCopy() to get a mutable value list
  • Add the desired phone number to the mutable value list with ABMultiValueAddValueAndLabel()
  • Write the mutable value list with the phone number added back to the record with ABRecordSetValue()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM