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

时间:2012-08-17 09:37:46

标签: ios abaddressbook abpeoplepickerview

我要做的是向用户展示人物选择器,让他选择他想要的所有联系人,最后将所有这些联系人的电子邮件地址放入阵列中。 最好的方法是只显示与用户发送电子邮件的联系人。

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

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>."

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分: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;
}

它允许用户选择特定的电子邮件并解除控制器没有任何错误。

答案 1 :(得分: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;
}

相关问题: