简体   繁体   English

如何使用 ABPeoplePicker 获得 email?

[英]How to get an email using ABPeoplePicker?

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:我没有发现 Apple 的文档对于实际使用人员选择器获取数据很有帮助,而且互联网上似乎没有太多其他信息:(我想我需要在这个 function 中获取 email:

- (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?我可以在里面放什么来获得所选人员的 email?

Kal answer is actually inaccurate - namely because "ABMultiValueCopyValueAtIndex" takes an index not identifier. Kal 的答案实际上是不准确的 - 即因为“ABMultiValueCopyValueAtIndex”采用索引而不是标识符。

Identifier value is static (like enumeration)标识符值为 static (如枚举)

  • "Home Email" is always "0" “家庭电子邮件”始终为“0”
  • "Work Email" is always "1". “工作电子邮件”始终为“1”。

So it will crash when the person selected only have 1 email stored, which is a "Work Email". 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".由于标识符是“1”,但我们需要索引“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;

Use利用

ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);

After that, you can use the ABMultiValueRefs API method calls to get email address.之后,您可以使用 ABMultiValueRefs API 方法调用来获取 email 地址。

EDIT -- This should give you email编辑——这应该给你 email

CFStringRef emailId = ABMultiValueCopyValueAtIndex(emails, identifier);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM