Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kABPersonEmailProperty returns weird stuff

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.

like image 616
sumderungHAY Avatar asked Jul 12 '11 01:07

sumderungHAY


2 Answers

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.

like image 115
omz Avatar answered Sep 19 '22 13:09

omz


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;
}
like image 45
Schultz9999 Avatar answered Sep 21 '22 13:09

Schultz9999