我希望能够从 ABPeoplePickerNavigationController 中选择一条记录,然后从我的视图控制器中向该联系人添加一封电子邮件。
我试图搜索 ABAddressBook 类参考,看看我能做什么,但没有任何见解 =/
我被人参考困在这里,有人知道如何进行吗?
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
我在使用时遇到 EXC_BAD_ACCESSABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFStringRef)@"a@a.com", &error);
但没有日志错误。
更新
好的,我设法将它保存在 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]
第一个日志似乎没问题,因为它是在 delegates 方法中完成的,但第二个很奇怪,我的意思是内存引用在那里但不是内容......第二个是在 peoplePicker 被解雇后记录的......
为什么会这样?
我的 .h 文件:
@property (nonatomic) ABAddressBookRef addressBook;
@property (nonatomic) ABRecordRef selectedPerson;