1

This works fine when I select contacts that have multiple phone numbers, and pick one of their phone numbers, recipientAddress is set to the selected phone number. But when I select email addresses from contacts having multiple email addresses, the ABMultiValueIdentifier is zero, and it translates into an index of zero, which is always the last email in the contact, regardless of which I selected.

I must be doing something embarrassingly wrong and easy to find, so please make yourself look great by exposing my foolishness.

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    @try {
        [eta addRecipient: person : property: identifier];
    }
    @catch (NSException *exception) {
        errExcLog(exception);
    }
    return NO;
}

- (void) addRecipient : (ABRecordRef) person : (ABPropertyID) property : (ABMultiValueIdentifier)identifier {
    ABMultiValueRef mvPropertyRef = ABRecordCopyValue(person,  property);
    int propertyIndex = ABMultiValueGetIndexForIdentifier( mvPropertyRef,  identifier);
    NSString *recipientAddress = (__bridge NSString *)(ABMultiValueCopyValueAtIndex( mvPropertyRef,  propertyIndex));
}
SafeFastExpressive
  • 3,637
  • 2
  • 32
  • 39

1 Answers1

4

These two methods might help you to get the selected person's email id

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
  shouldContinueAfterSelectingPerson:(ABRecordRef)person
                            property:(ABPropertyID)property
                          identifier:(ABMultiValueIdentifier)identifier
{
    ABPersonViewController *controller = [[ABPersonViewController alloc] init];
    controller.displayedPerson = person;
    controller.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonEmailProperty]];
    controller.personViewDelegate = self;
    [peoplePicker pushViewController:controller animated:YES];
    [controller release];
    return NO;
}

-(BOOL)personViewController:(ABPersonViewController *)personViewController
                shouldPerformDefaultActionForPerson:(ABRecordRef)person
                property:(ABPropertyID)property
              identifier:(ABMultiValueIdentifier)identifierForValue
{
    ABMutableMultiValueRef multiEmail = ABRecordCopyValue(person, property);

    NSString *emailAddress = (NSString *) ABMultiValueCopyValueAtIndex(multiEmail, identifierForValue);

    NSLog(@"strEmail %@",emailAddress);

    ABPeoplePickerNavigationController *peoplePicker = (ABPeoplePickerNavigationController *)personViewController.navigationController;
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return NO;
}
D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30
  • First, let me apologize for taking so long to respond. I didn't have email notifications on and forgot I posted this. Your code works, but suddenly now so does mine (no changes). I'm going to chalk this up to an iOS 7 bug that got magically fixed in the GM (or a mysterious side affect of some bug in my other code). I'm confused why a parameter defined as ABMultiValueIdentifier can be used directly as a ABMultiValueIndex, but as a long time ABAddressBook developer I know how silly it is to assume ABAddressBook APIs make logical sense. Thanks for your help. – SafeFastExpressive Sep 21 '13 at 20:01
  • @RandyHill : that's great that finally you got everything working because end of the day that does matter nothing else. and thanks for accepting my answer. :) – D-eptdeveloper Sep 23 '13 at 04:06