地址簿 - 选择已链接联系人的电子邮件

时间:2014-04-14 12:25:08

标签: ios objective-c abaddressbook peoplepicker

我正在尝试在下面提到的委托回调中获取所选的电子邮件属性

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
        if (property==kABPersonEmailProperty) {
            ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
            if (ABMultiValueGetCount(emails) > 0) {
                NSString *email = (__bridge_transfer NSString*)
                ABMultiValueCopyValueAtIndex(emails, ABMultiValueGetIndexForIdentifier(emails,identifier));
                [recipientEmail setText:email];
                [peoplePicker dismissViewControllerAnimated:YES completion:nil];
            }
             CFRelease(emails);
        }
        return NO;
    }

但是,如果我选择链接联系人的电子邮件属性(拥有单个电子邮件),我将标识符设置为0,因此我获得了主要联系人的第一个电子邮件ID。     例如:John - john@gmail.com                john26@gmail.com         Roger(Linked Contact) - roger@gmail.com

当我选择roger@gmail.com时,我得到john@gmail.com。

1 个答案:

答案 0 :(得分:0)

我有同样的问题。当我选择包含多个电子邮件地址的帐户时,收到错误的电子邮件当我遍历所选的ABMutableMultiValueRef ...

for (CFIndex ix = 0; ix < ABMultiValueGetCount(emails); ix++) {
   CFStringRef label = ABMultiValueCopyLabelAtIndex(emails, ix);
   CFStringRef value = ABMultiValueCopyValueAtIndex(emails, ix);
   NSLog(@"I have a %@ address: %@", label, value);
   if (label) CFRelease(label);
   if (value) CFRelease(value);
}

...我多次出现相同的地址,但有些地址为空标签。

> I have a (null) address: john@home.com
> I have a _$!<Work>!$_ address: jsmith@work.com
> I have a _$!<Home>!$_ address: john@home.com

我将尝试的解决方法是首先过滤掉空标签,看看ABMultiValueIdentifier是否适合,如果不是还原为空标签。除非你找到了什么?

编辑:这对我有用。

    NSMutableArray *labeled = [NSMutableArray new];
    ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    for (CFIndex ix = 0; ix < ABMultiValueGetCount(emails); ix++) {
        CFStringRef label = ABMultiValueCopyLabelAtIndex(emails, ix);
        CFStringRef value = ABMultiValueCopyValueAtIndex(emails, ix);
        if (label != NULL) {
            [labeled addObject:(NSString *)CFBridgingRelease(value)];
        }
        if (label) CFRelease(label);
        if (value) CFRelease(value);
    }
    NSString *email;
    if (labeled.count > identifier) {
        email = [labeled objectAtIndex:identifier];
    } else {
        CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, identifier);
        email = (NSString *)CFBridgingRelease(emailRef);
    }