iOS在数组中获取所选联系人的电子邮件地址

我想要做的是向用户显示人员select器,让他select他想要的所有联系人,最后将所有联系人的电子邮件地址都放在一个数组中。 最好的是只显示给用户的电子邮件联系人。

直到现在,我所能做的唯一事情就是向用户提供这个代码:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; picker.peoplePickerDelegate = self; [self presentModalViewController:picker animated:YES]; 

然后我试图使用此代码来获取选定的联系人的电子邮件:

 - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty); [email addObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, 0)]; [self dismissModalViewControllerAnimated:YES]; return YES; } 

但是,一旦我select一个联系人,选取器就会消失,所以我不知道如何继续。 此外,当我select一个联系人时,我在控制台中得到这个:

 "Unbalanced calls to begin/end appearance transitions for <ABMembersViewController: 0xa1618c0>." 

任何帮助,将不胜感激。

我不知道你是否曾经解决过你的问题,但如果有人发现这个post,也许会帮助他们。 我所做的从ABPeoplePickerNavigationController获取电子邮件是删除

 [self dismissModalViewControllerAnimated:YES]; 

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

然后我使用这个来获取电子邮件并解散控制器

 - (BOOL)peoplePickerNavigationController(ABPeoplePickerNavigationController*)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { if (kABPersonEmailProperty == property) { ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty); NSString *email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(multi, 0); NSLog(@"email: %@", email); [self dismissModalViewControllerAnimated:YES]; return NO; } return YES; } 

它允许用户select一个特定的电子邮件和解散控制器没有任何错误。

据我所知,这实际上不会给你所select的电子邮件地址。 如果一个联系人有“家庭”和“工作”的电子邮件,那么ABMultiValueCopyValueAtIndex(multi, 0)只会给你“家庭”的电子邮件。

您需要从标识符中获取所选电子邮件的索引。

 - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { if (property == kABPersonEmailProperty) { ABMultiValueRef emails = ABRecordCopyValue(person, property); CFIndex ix = ABMultiValueGetIndexForIdentifier(emails, identifier); CFStringRef email = ABMultiValueCopyValueAtIndex(emails, ix); [self dismissViewControllerAnimated:YES completion:nil]; [self callMethodWithEmailString:(__bridge NSString *)(email)]; if (email) CFRelease(email); if (emails) CFRelease(emails); } return NO; } 

相关问题:

  • 如何使用ABPeoplePicker获取电子邮件?
  • 如何从ABPeoplePicker获取用户select的电子邮件地址?