如何获取用户在iOS通讯录中选择的电子邮件值?

时间:2013-05-06 01:36:38

标签: iphone ios addressbook

我正在尝试获取通讯录联系人的电子邮件值。

我需要捕获用户点击的电子邮件,我可以弄清楚如何正确使用语法。

我使用了一个选择了第一封电子邮件的示例,但我现在正尝试将其切换为用户选择的电子邮件。我开始写// NSString * email = ABRecordCopyValue(property,);

或如何获取用户选择的索引值。如果我有所选电子邮件的索引值,我可以重用我已注释掉的代码。

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

    NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    self.userName.text = name;

    self.myMessage.recpipientName = name;

    //NSString* email = ABRecordCopyValue(property)

    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);

    //    if (ABMultiValueGetCount(emails) > 0) {

    //        email = (__bridge_transfer NSString*)

    //        ABMultiValueCopyValueAtIndex(emails, 0);

    //    } else {

    //        email = @"[None]";

    //    }

    self.userEmail.text = email;
    self.myMessage.recipientEmail = email;
    CFRelease(emails);
    return NO;
}

1 个答案:

答案 0 :(得分:4)

这是直接来自我的书(http://www.apeth.com/iOSBook/ch31.html#_abpeoplepickernavigationcontroller)的示例代码:

- (BOOL)peoplePickerNavigationController:
        (ABPeoplePickerNavigationController *)peoplePicker
        shouldContinueAfterSelectingPerson:(ABRecordRef)person
        property:(ABPropertyID)property
        identifier:(ABMultiValueIdentifier)identifier {
    ABMultiValueRef emails = ABRecordCopyValue(person, property);
    CFIndex ix = ABMultiValueGetIndexForIdentifier(emails, identifier);
    CFStringRef email = ABMultiValueCopyValueAtIndex(emails, ix);
    NSLog(@"%@", email); // do something with the email here
    if (email) CFRelease(email);
    if (emails) CFRelease(emails);
    [self dismissViewControllerAnimated:YES completion:nil];
    return NO;
}