2

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

到目前为止,我唯一能做的就是向人员选择器提供以下代码:

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;
}

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

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

任何帮助,将不胜感激。

4

2 回答 2

7

我不确定你是否解决了你的问题,但如果其他人发现这篇文章也许会对他们有所帮助。我从 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;
}

它允许用户选择特定的电子邮件并关闭控制器而不会出现任何错误。

于 2012-10-31T20:57:22.743 回答
3

据我所知,这实际上不会给您选择的电子邮件地址。如果联系人有“家庭”和“工作”电子邮件,那么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;
}

相关问题:

于 2014-03-11T16:19:59.400 回答