2011-03-20 131 views
15

因此,我有一种方法可以从iPhone上的地址簿中获取所有联系人电话号码,但有没有办法获取电话号码标签?例如,你可以这样做: enter image description here从地址簿中获取iPhone电话号码标签

而我会修改我的方法来打印出标签(如iPhone/Home/mobile/etc)。

ABAddressBookRef addressBook = ABAddressBookCreate(); 
CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook); 
CFIndex n = ABAddressBookGetPersonCount(addressBook); 

for(int i = 0 ; i < n ; i++) 
{ 
    ABRecordRef ref = CFArrayGetValueAtIndex(all, i); 
    NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
    NSLog(@"Name %@", firstName); 

    ABMultiValueRef *phones = ABRecordCopyValue(ref, kABPersonPhoneProperty); 
    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) 
    { 
     NSString *phoneLabel = @""; // ??? 

     CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j); 
     //CFRelease(phones); 
     NSString *phoneNumber = (NSString *)phoneNumberRef; 
     CFRelease(phoneNumberRef); 
     NSLog(@" - %@ (%@)", phoneNumber, phoneLabel); 
     [phoneNumber release]; 
    } 
} 

回答

60

只需使用 -

ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty); 
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) 
{ 
    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j); 
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j); 
    NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 
    //CFRelease(phones); 
    NSString *phoneNumber = (NSString *)phoneNumberRef; 
    CFRelease(phoneNumberRef); 
    CFRelease(locLabel); 
    NSLog(@" - %@ (%@)", phoneNumber, phoneLabel); 
    [phoneNumber release]; 
} 

编辑 请参阅笔记这个答案约CFBridgingRelease__bridge_transfer

+0

谢谢,这样做! – 2011-03-21 10:06:18

+0

欢迎您,很高兴它帮助 – shannoga 2011-03-21 10:09:40

+3

而不是使用(NSString *)转换为字符串使用(__bridge_transfer NSString *)。 – Dev2rights 2013-02-05 15:28:41

-4

以下应该有所帮助:

NSArray* AccountEmailAddresses(void) 
{ 
    NSMutableArray *emailAddresses = [NSMutableArray array]; 
    @try 
    { 
     Class MailComposeController = NSClassFromString(@"MailComposeController") ?: NSClassFromString(@"MFMailComposeController"); 
     NSArray *accountEmailAddresses = [MailComposeController performSelector:@selector(accountEmailAddresses)]; 
     for (id address in accountEmailAddresses) 
     { 
      if ([address isKindOfClass:[NSString class]]) 
       [emailAddresses addObject:address]; 
     } 
    } 
    @catch (NSException *e) {} 

    return [NSArray arrayWithArray:emailAddresses]; 
} 


ABRecordRef ABGetMe(ABAddressBookRef addressBook) 
{ 
    ABRecordRef me = NULL; 
    NSArray *accountEmailAddresses = AccountEmailAddresses(); 
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex peopleCount = CFArrayGetCount(people); 
    for (CFIndex i = 0; i < peopleCount; i++) 
    { 
     ABRecordRef record = CFArrayGetValueAtIndex(people, i); 
     ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty); 
     if (emails) 
     { 
      CFIndex emailCount = ABMultiValueGetCount(emails); 
      for (CFIndex j = 0; j < emailCount; j++) 
      { 
       CFStringRef email = ABMultiValueCopyValueAtIndex(emails, j); 
       if (email) 
       { 
        if ([accountEmailAddresses containsObject:(id)email]) 
         me = record; 

        CFRelease(email); 
       } 
       if (me) 
        break; 
      } 
      CFRelease(emails); 
     } 
     if (me) 
      break; 
    } 

    return me; 
} 
+2

是什么?用户询问有关电话号码,而不是电子邮件地址 – lensovet 2012-09-07 01:52:51

3
//get the particular contact or email from phone book 

    - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)picker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
    { 
     // Name of contact. 

     NSString* name = (NSString *)ABRecordCopyCompositeName(person); 

     // Numbers of selected contact 

     ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); 

     NSMutableString *mobile = [[NSMutableString alloc] init]; 
     NSMutableString *office = [[NSMutableString alloc] init]; 

     // Getting if Mobile, Office(work) numbers exist 

     for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++) 
     { 
      // Number in contact details of current index 

     CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, numberIndex); 

     // Label of Phone Number 

     CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, numberIndex); 
     NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 

     // Phone number 

     NSString *phoneNumber = (NSString *)phoneNumberRef; 

     // Release Phone Number and locationLabel reference object 

     CFRelease(phoneNumberRef); 
     CFRelease(locLabel); 

     NSLog(@" - %@ (%@)", phoneNumber, phoneLabel); 

     if ([phoneLabel isEqualToString:NSLocalizedString(@"mobile", nil)])// Mobile number saving. 
     { 
      [mobile appendFormat:@"%@", phoneNumber]; 
     } 
     else if ([phoneLabel isEqualToString:NSLocalizedString(@"work", nil)])// Office number saving. 
     { 
      [office appendFormat:@"%@", phoneNumber]; 
     } 

     [phoneNumber release]; 
    } 
    CFRelease(phones); 

    // Emails of selected contact 

    ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); 

    NSMutableString *generalMail = [[NSMutableString alloc] init]; 
    NSMutableString *officeMail = [[NSMutableString alloc] init]; 

    // Getting if Home, Office(work) mails exist 

    for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(emails); numberIndex++) 
    { 
     // Mail in contact details of current index 

     CFStringRef mailRef = ABMultiValueCopyValueAtIndex(emails, numberIndex); 

     // Label of Phone Number 

     CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(emails, numberIndex); 
     NSString *mailLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 

     // Phone number 

     NSString *mail = (NSString *)mailRef; 

     // Release Phone Number and locationLabel reference object 

     CFRelease(mailRef); 
     CFRelease(locLabel); 
     NSLog(@" - %@ (%@)", mail, mailLabel); 

     if ([mailLabel isEqualToString:NSLocalizedString(@"mobile", nil)])// Home mail. 
     { 
      [generalMail appendFormat:@"%@", mail]; 
     } 
     else if ([mailLabel isEqualToString:NSLocalizedString(@"work", nil)])// Office(Work) mail. 
     { 
      [officeMail appendFormat:@"%@", mail]; 
     } 

     [mail release]; 
    } 
    CFRelease(emails); 

    [mobile release]; 
    [office release]; 

    [generalMail release]; 
    [officeMail release]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
    return NO; 
} 
+0

如果此代码与deutsch一起运行,那么“mobile”会变成“mobil”! 。我该如何处理这种情况?我可能无法检查所有字符串 – BaSha 2014-11-17 12:12:56

+0

好的。在您的项目中创建所需语言的本地化文件。在该文件中定义mobile ='mpbiel'。我正在修改根据上述代码。 – abhi 2014-12-04 06:05:01

+0

其实我已经解决了问题,我们需要比较本地标签,而不是本地化的标签 - >让locLabel:CFStringRef = ABMultiValueCopyLabelAtIndex(phones,numberIndex).takeUnretainedValue()as CFStringRef; if(String(locLabel)== String(kABHomeLabel)){} – BaSha 2014-12-04 09:01:56

0

如果要添加记录到地址簿,这些预定义的常量可能是你想要的东西,kABPersonPhoneMobileLabelkABPersonPhoneIPhoneLabel,这是在文件中定义ABPerson.h 。

相关问题