我的方案是从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;
}
此代码也很完美,我在所需的文本字段中获取值。
然而,当我将这两个代码组合起来时,i-e在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;
}
问题是只有这个号码被添加到联系人中,并且所有其他现有条目都被删除。这个号码会覆盖所有其他现有条目。 请帮忙
答案 0 :(得分:4)
ABRecordSetValue()
将记录的属性设置为您提供的值,覆盖所述属性的任何现有值。由于您使用ABMultiValueCreateMutable()
创建新的空值列表,因此您实际上会忽略kABPersonPhoneProperty
上可能已存在的ABRecord
的任何值。
相反,您编辑现有记录的过程应为:
ABRecordCopyValue()
以获取现有值列表ABMultiValueCreateMutableCopy()
以获取可变值列表ABMultiValueAddValueAndLabel()
ABRecordSetValue()