简体   繁体   English

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

[英]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. 我的方案是从iPhone的地址簿中选择一个联系人,并将其名称和第一个电话号码显示在文本字段中,同时以编程方式在其KABOtherLabel属性中添加电话号码。

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 但是当我组合这两个代码时,即在displayPerson()方法的末尾写下面的代码或者从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. ABRecordSetValue() 记录的属性设置为您给它的值,覆盖所述属性的任何现有值。 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 . 因为您使用ABMultiValueCreateMutable()来创建新的空值列表,所以您实际上忽略了ABRecord上可能已存在的任何kABPersonPhoneProperty值。

Instead, your process for editing an existing record should be: 相反,您编辑现有记录的过程应该是:

  • Call ABRecordCopyValue() on a chosen record to get the existing value list 在所选记录上调用ABRecordCopyValue()以获取现有值列表
  • Call ABMultiValueCreateMutableCopy() to get a mutable value list 调用ABMultiValueCreateMutableCopy()以获取可变值列表
  • Add the desired phone number to the mutable value list with ABMultiValueAddValueAndLabel() 使用ABMultiValueAddValueAndLabel()将所需的电话号码添加到可变值列表中
  • Write the mutable value list with the phone number added back to the record with ABRecordSetValue() 使用ABRecordSetValue()将带有电话号码的可变值列表写回记录中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从地址簿联系人处获取电话号码(iphone sdk) - How to get a Phone Number from an Address Book Contact (iphone sdk) 如何从网页添加联系人到 iPhone 的通讯录? - How to add a contact to the iPhone's Address Book from a Web Page? 从地址簿获取iPhone电话号码标签 - Get iPhone phone number label from Address Book 从设备中的iPhone通讯录中获取电话号码错误 - Get phone number from iPhone address book ERROR in device Iphone 将联系人添加到地址簿中的现有群组 - Iphone Add contact to existing group in address book 如何编写代码以从电话簿中选择选定的电话号码地址以从iPhone应用程序发送消息 - How to write code to pick the selected phone num address from phone book to send msg from my iphone application 当新联系人添加到iPhone上的通讯录时,它的名字属性NSLogs为null - When new contact is added to address book on iPhone, it's first name property NSLogs as null iPhone SDK:从电话号码获取联系人姓名 - iPhone SDK: Getting Contact's Name from the Phone Number 如何编写代码以从电话簿中选择选定的电子邮件地址,以从我的iPhone应用程序发送电子邮件 - How to write code to pick the selected EMail address from phone book to send E-mail from my iphone application ABAddressBook - 如何查找是否从iPhone的地址簿中修改或删除了特定联系人? - ABAddressBook - How to find if a particular contact is modified or deleted from iPhone's address book?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM