如何使用 ABPeoplePicker 获取电子邮件?

发布于 11-25 09:51 字数 360 浏览 4 评论 0原文

我没有发现苹果的文档对于实际使用人员选择器获取数据非常有帮助,而且互联网上似乎没有太多其他信息:(我认为我需要在此功能中获取电子邮件:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{

}

我可以放什么在那里获取所选人员的电子邮件?

I'm not finding Apple's documentation very helpful for actually getting data with a people picker, and there doesn't seem to be much other information on the internet :( I assume I need to get the email in this function:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{

}

What can I put in there to get the email of the selected person?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光2024-12-02 09:51:28

Kal 的答案实际上是不准确的 - 即因为“ABMultiValueCopyValueAtIndex”采用索引而不是标识符。

标识符值是静态的(如枚举)

  • “家庭电子邮件”始终为“0”
  • “工作电子邮件”始终为“1”。

因此,当所选人员仅存储 1 封电子邮件(即“工作电子邮件”)时,它会崩溃。由于标识符是“1”,但我们需要索引“0”。

幸运的是,我们可以使用以下来获取索引:

int index = ABMultiValueGetIndexForIdentifier(emails, identifier);

代码:

if (property == kABPersonEmailProperty) {

    ABMultiValueRef emails = ABRecordCopyValue(person, property);

    NSString *count = [NSString stringWithFormat:@"Count: %d Identifier: %d", ABMultiValueGetCount(emails), identifier];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:count delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    if(ABMultiValueGetCount(emails) > 0)
    {
        int index = ABMultiValueGetIndexForIdentifier(emails, identifier);
        CFStringRef emailTypeSelected = ABMultiValueCopyLabelAtIndex(emails, index);
        CFStringRef emailTypeSelectedLocalized = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emails, index));
        CFStringRef emailValueSelected = ABMultiValueCopyValueAtIndex(emails, index);

        self.lblEmailType.text = (NSString *) emailTypeSelected;
        self.lblEmailTypeLocalized.text = (NSString *) emailTypeSelectedLocalized;
        self.lblEmailValue.text = (NSString *) emailValueSelected;
    }

    [ self dismissModalViewControllerAnimated:YES ];
    return NO;
}

return YES;

Kal answer is actually inaccurate - namely because "ABMultiValueCopyValueAtIndex" takes an index not identifier.

Identifier value is static (like enumeration)

  • "Home Email" is always "0"
  • "Work Email" is always "1".

So it will crash when the person selected only have 1 email stored, which is a "Work Email". Since the identifier is "1", but we need index "0".

Luckily we can use following to get the index:

int index = ABMultiValueGetIndexForIdentifier(emails, identifier);

Code:

if (property == kABPersonEmailProperty) {

    ABMultiValueRef emails = ABRecordCopyValue(person, property);

    NSString *count = [NSString stringWithFormat:@"Count: %d Identifier: %d", ABMultiValueGetCount(emails), identifier];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:count delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    if(ABMultiValueGetCount(emails) > 0)
    {
        int index = ABMultiValueGetIndexForIdentifier(emails, identifier);
        CFStringRef emailTypeSelected = ABMultiValueCopyLabelAtIndex(emails, index);
        CFStringRef emailTypeSelectedLocalized = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emails, index));
        CFStringRef emailValueSelected = ABMultiValueCopyValueAtIndex(emails, index);

        self.lblEmailType.text = (NSString *) emailTypeSelected;
        self.lblEmailTypeLocalized.text = (NSString *) emailTypeSelectedLocalized;
        self.lblEmailValue.text = (NSString *) emailValueSelected;
    }

    [ self dismissModalViewControllerAnimated:YES ];
    return NO;
}

return YES;
青春如此纠结2024-12-02 09:51:28

使用

ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);

之后,您可以使用 ABMultiValueRefs API 方法调用来获取电子邮件地址。

编辑 - 这应该给你电子邮件

CFStringRef emailId = ABMultiValueCopyValueAtIndex(emails, identifier);

Use

ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);

After that, you can use the ABMultiValueRefs API method calls to get email address.

EDIT -- This should give you email

CFStringRef emailId = ABMultiValueCopyValueAtIndex(emails, identifier);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文