2014-04-14 130 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); 
    } 
相關問題