iOS Getting selected contacts' email address in array

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 16:57:06

I am not sure if you ever solved your problem but if anyone else finds this post maybe it will help them. What I did to get an email from the ABPeoplePickerNavigationController was to remove

[self dismissModalViewControllerAnimated:YES];

from

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person;

and then I am using this to get the email and dismiss the controller

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

It allows the user to select a specific email and dismisses the controller without any errors.

davegaeddert

As far as I can tell, this won't actually give you the email address that was selected. If a contact has "home" and "work" emails then ABMultiValueCopyValueAtIndex(multi, 0) will just give you the "home" email.

You need to get the index for the selected email from the identifier.

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

Related Questions:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!