kABPersonEmailProperty returns weird stuff
Asked Answered
T

3

8

I am tring to get email address of ABRecordRef like this:

ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
NSString *email = [(NSString*) ABRecordCopyValue( ref, kABPersonEmailProperty ) autorelease];
NSLog(@"%@", email);

It returning this:

_$!<Home>!$_ (0x6840af0) - [email protected] (0x6840cc0)

What's this stuff around the email? and how can I get rid of it?Thanks.

Tradition answered 12/7, 2011 at 1:35 Comment(0)
P
31

kABPersonEmailProperty is of type kABMultiStringPropertyType. There is no single email address property, a person might have an email address for work, one for home, etc. You can get an array of all email addresses by using ABMultiValueCopyArrayOfAllValues:

ABMultiValueRef emailMultiValue = ABRecordCopyValue(ref, kABPersonEmailProperty);
NSArray *emailAddresses = [(NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue) autorelease];
CFRelease(emailMultiValue);

To get the labels of the email addresses, use ABMultiValueCopyLabelAtIndex. "_$!<Home>!$" is a special constant that's defined as kABHomeLabel, there's also kABWorkLabel.

Passage answered 12/7, 2011 at 10:10 Comment(1)
and it's possible to know the tag of that email? if is from work, personal, etc??Booty
M
1

Basically more details for @omz answer. Here is the code I used that extracts home email and the name of the person:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    for (CFIndex i = 0; i < ABMultiValueGetCount(emails); i++) {
        NSString *label = (__bridge NSString *) ABMultiValueCopyLabelAtIndex(emails, i);
        if ([label isEqualToString:(NSString *)kABHomeLabel]) {
            NSString *email = (__bridge NSString *) ABMultiValueCopyValueAtIndex(emails, i);
            _emailTextField.text = email;
        }
    }
    CFRelease(emails);


    NSString *first = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *last = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

    if (first && first.length > 0 && last && last.length > 0) {
        _nicknameTextField.text = [NSString stringWithFormat:@"%@ %@", first, last];
    } else if (first && first.length > 0) {
        _nicknameTextField.text = first;
    } else {
        _nicknameTextField.text = last;
    }

    [self dismissModalViewControllerAnimated:YES];

    return NO;
}
Maxfield answered 31/8, 2013 at 6:41 Comment(1)
how can I implement this in Swift how can I loop CFIndexJezebel
F
0

Try out this......

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:
[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
picker.displayedProperties = displayedItems;
Frutescent answered 12/7, 2011 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.