如何在ABAddressBook上编辑记录

时间:2013-01-14 13:47:09

标签: ios objective-c abaddressbook

我希望能够从ABPeoplePickerNavigationController中选择一条记录,然后从我的视图控制器向该联系人添加一封电子邮件。

我试图搜索ABAddressBook类引用,看看我能做什么,但没有见解= /

我被困在这里的人参考,有人知道继续进行的方式吗?

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

使用ABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFStringRef)@"a@a.com", &error);时我有一个EXC_BAD_ACCESS,但没有记录错误。

更新

好吧,我设法将它保存在peoplePicker委托中:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    ...

    self.addressBook = peoplePicker.addressBook;
    self.selectedPerson = person;

    // adding to native contacts
    CFErrorRef error = nil;
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
    bool didAddEmail = ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(email), kABOtherLabel, NULL);
    if(didAddEmail){
        ABRecordSetValue(self.selectedPerson, kABPersonEmailProperty, emailMultiValue, nil);
        NSLog(@"Email saved......");
    } else {
        NSLog(@"Error adding email: %@", error);
    }
    ABAddressBookSave(self.addressBook, NULL);

    ...
}

奇怪的是,如果我尝试在此方法之外执行此操作,例如在用户在textField上输入之后,我会收到错误。

然后在上面peoplePickerNavigationController方法的内部和外部检查我的日志:

(lldb) po self.selectedPerson
(ABRecordRef) $1 = 0x0a1838f0 <CPRecord: 0xa1838f0 ABPerson>
(lldb) po self.selectedPerson
(ABRecordRef) $4 = 0x0a1838f0 [no Objective-C description available]

第一个日志似乎没问题,因为它是在委托方法内部完成的,但第二个是奇怪的,我的意思是内存引用在那里而不是内容......第二个是在peoplePicker被解雇后记录的...

为什么会这样?

我的.h文件:

@property (nonatomic) ABAddressBookRef addressBook;
@property (nonatomic) ABRecordRef selectedPerson;

1 个答案:

答案 0 :(得分:1)

我的解决方案是保存人员ID,然后再次访问AddressBook并使用: ABAddressBookGetPersonWithRecordID(addressBook, self.selectedPersonID)

再次创建地址簿。

ABAddressBookRef addressBook = ABAddressBookCreate();