2014-04-14 94 views
1

我想在下面地址簿 - 链接联系人

-(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。 如:约翰 - [email protected] [email protected] 罗杰(链接联系人) - [email protected]

当我选择[email protected]我得到[email protected]

回答

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: [email protected] 
> I have a _$!<Work>!$_ address: [email protected] 
> I have a _$!<Home>!$_ address: [email protected] 

的解决方法,我会尝试是,先筛选出的空标签,看是否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); 
    } 
相关问题