我注意到我们用来为 ABPeoplePickerNavigationController 提供的联系人选择特定电子邮件的逻辑中的一个错误。当我们的联系人有来自 Twitter 或 Facebook 的链接联系人时,该错误就会发生。
我已经根据几个相关的 StackOverflow 问题仔细检查了我们的逻辑,我似乎在“正确”地做所有事情:
这是在点击按钮后加载 ABPeoplePicker 的代码:
- (void)setUpChooseContactButton
{
@weakify(self)
[self.chooseContactButton addEventHandler:^(id sender) {
@strongify(self)
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
picker.displayedProperties = @[@(kABPersonEmailProperty)];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
} forControlEvents:UIControlEventTouchUpInside];
}
稍后,当用户点击选定联系人的电子邮件时,我们会按如下方式处理它:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
if (property == kABPersonEmailProperty)
{
ABMultiValueRef multiEmail = ABRecordCopyValue(person, kABPersonEmailProperty);
CFIndex selectedIndex = ABMultiValueGetIndexForIdentifier(multiEmail, identifier);
CFStringRef selectedEmail = ABMultiValueCopyValueAtIndex(multiEmail, selectedIndex);
NSLog(@"Doing something with %@", selectedEmail);
if (selectedEmail) CFRelease(selectedEmail);
if (multiEmail) CFRelease(multiEmail);
}
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
当联系人没有任何链接的联系人时,这非常有效,例如 Facebook,但是,当它这样做时,此处打印的电子邮件地址将不是用户选择的电子邮件地址。
例如,我的联系人中有一个朋友,我们将其称为John Snow,我们是好朋友,所以我在 iCloud、2 个不同的 gmail 帐户和 Facebook 上与他保持联系。
当我导航到 John Snow 的联系页面时,我看到以下 5 封电子邮件:
- john.snow@gmail.com
- jsnow@gmail.com
- lordsnow@nightswatch.org
- john.snow@facebook.com
- john@winterfellstarks.com
以及每个链接联系人的单元格。
因此,如果我点击lordsnow@nightswatch.org(显示的电子邮件列表中的索引为 2)并在 NSLog 的 shouldContinueAfterSelecting 回调中设置断点并打印出相关变量,这就是我将看到的:
(lldb) po multiEmail
ABMultiValueRef 0x17737ca0 with 8 value(s)
0: null (0x3b864a60) - john@winterfellstarks.com (0x15d4bd30)
1: null (0x3b864a60) - lordsnow@nightswatch.org (0x15f0f2d0)
2: null (0x3b864a60) - john.snow@gmail.com (0x15f3b470)
3: null (0x3b864a60) - jsnow@gmail.com (0x15d02060)
4: _$!<Work>!$_ (0x15f352d0) - john.snow@facebook.com (0x15f3d480)
5: _$!<Other>!$_ (0x15fbeae0) - john.snow@gmail.com (0x15f2a170)
6: _$!<Other>!$_ (0x15de9ce0) - jsnow@gmail.com (0x15f0f0e0)
7: _$!<Other>!$_ (0x15f0a110) - lordsnow@nightswatch.org (0x15f00830)
(lldb) po selectedIndex
2
(lldb) po identifier
2
(lldb) po selectedEmail
john.snow@gmail.com
我真的很感激这里的一些帮助。