开发者

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) - test@test.com (0x6840cc0)

What's this stuff around the email? an开发者_开发问答d how can I get rid of it?Thanks.


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.


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜