从iPhone通讯录中选择一个联系人并添加新的电话号码

4

我的场景是从iPhone的通讯录中选择联系人,并将其姓名和第一个电话号码显示在文本字段中,同时以编程方式添加电话号码到其KABOtherLabel属性中。

我正在使用以下代码以编程方式添加联系人;

-(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...");

  }

}

这段代码运行得非常完美。

我正在使用以下代码来选择联系人并将其显示在文本字段中:

-(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;




}

这段代码也完美地运行了,我在所需的文本字段中获取了值。

然而,当我将这两个代码组合起来时,即在displayPerson()方法的末尾编写以下代码或从shouldContinueAfterSelectingPerson中调用它时

我用来添加现有联系人详细信息的代码如下:

   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;

问题在于,只有这个号码被添加到联系人中,所有其他现有的条目都被删除了。也就是说,这个号码覆盖了所有其他现有的条目。请帮忙解决。


请耐心阅读此内容,不要立即将其评为差评,因为我花了足够的时间阅读了所有关于编程添加联系人的文章,但没有找到任何关于覆盖现有联系人的解决方案。 - user1940888
1个回答

4
ABRecordSetValue()会将记录的属性设置为您给出的值,并覆盖该属性的任何现有值。因为您使用ABMultiValueCreateMutable()创建了一个新的空值列表,所以您实际上忽略了可能已经存在于ABRecord上的kABPersonPhoneProperty值。
相反,您编辑现有记录的过程应该是:
  • 调用ABRecordCopyValue()在选择的记录上获取现有的值列表
  • 调用ABMultiValueCreateMutableCopy()获取可变的值列表
  • 使用ABMultiValueAddValueAndLabel()向可变的值列表添加所需的电话号码
  • 使用ABRecordSetValue()将包含添加了电话号码的可变值列表写回到记录中

太棒了!像冠军一样工作!谢谢你! :) - user1940888

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接