iphone - 从 iPhone 的地址簿中选择一个联系人并在其中添加一个新的电话号码

标签 iphone ios objective-c cocoa-touch

我的方案是从 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;

问题是只有这个号码被添加到联系人中,所有其他现有条目都被删除。即这个数字会覆盖所有其他现有条目。 请帮忙

最佳答案

ABRecordSetValue() 记录的属性设置为您给它的值,覆盖该属性的任何现有值。因为您使用 ABMultiValueCreateMutable() 创建一个新的空值列表,所以您实际上忽略了可能已经存在于 ABRecord 上的 kABPersonPhoneProperty 的任何值.

相反,您编辑现有记录的过程应该是:

  • 对所选记录调用 ABRecordCopyValue() 以获取现有值列表
  • 调用ABMultiValueCreateMutableCopy()获取可变值列表
  • 使用 ABMultiValueAddValueAndLabel() 将所需的电话号码添加到可变值列表中
  • 使用 ABRecordSetValue()
  • 将电话号码添加回记录的可变值列表

关于iphone - 从 iPhone 的地址簿中选择一个联系人并在其中添加一个新的电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14690006/

相关文章:

ios - UIImage 滑动指示器大小

iphone - 我可以在 9 月之前学习 iPhone SDK 并完成一个应用程序吗?

ios - Xcode |提取开发者信息时出错

ios - 如何将未知人员 ViewController 与最初输入的生成数据一起使用?

ios - 如何在 iTunes Connect 中使用已删除的应用名称?

iOS 将 subview 带回

iphone - 本地化应用的应用商店审核时间是否显着延长?

iphone - 在 View IOS 中禁用用户交互

ios - SwiftUI在FetchRequest中使用带有结构参数的关系谓词

iphone - 在 Objective C 中检索 Apple Id(或某种帐户 ID)