How to get an email using ABPeoplePicker?

前端 未结 2 1895
礼貌的吻别
礼貌的吻别 2021-01-23 22:38

I\'m not finding Apple\'s documentation very helpful for actually getting data with a people picker, and there doesn\'t seem to be much other information on the internet :( I as

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-23 23:17

    Kal answer is actually inaccurate - namely because "ABMultiValueCopyValueAtIndex" takes an index not identifier.

    Identifier value is static (like enumeration)

    • "Home Email" is always "0"
    • "Work Email" is always "1".

    So it will crash when the person selected only have 1 email stored, which is a "Work Email". Since the identifier is "1", but we need index "0".

    Luckily we can use following to get the index:

    int index = ABMultiValueGetIndexForIdentifier(emails, identifier);
    

    Code:

    if (property == kABPersonEmailProperty) {
    
        ABMultiValueRef emails = ABRecordCopyValue(person, property);
    
        NSString *count = [NSString stringWithFormat:@"Count: %d Identifier: %d", ABMultiValueGetCount(emails), identifier];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:count delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    
        if(ABMultiValueGetCount(emails) > 0)
        {
            int index = ABMultiValueGetIndexForIdentifier(emails, identifier);
            CFStringRef emailTypeSelected = ABMultiValueCopyLabelAtIndex(emails, index);
            CFStringRef emailTypeSelectedLocalized = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emails, index));
            CFStringRef emailValueSelected = ABMultiValueCopyValueAtIndex(emails, index);
    
            self.lblEmailType.text = (NSString *) emailTypeSelected;
            self.lblEmailTypeLocalized.text = (NSString *) emailTypeSelectedLocalized;
            self.lblEmailValue.text = (NSString *) emailValueSelected;
        }
    
        [ self dismissModalViewControllerAnimated:YES ];
        return NO;
    }
    
    return YES;
    

提交回复
热议问题