2011-03-20 8 views
15

아이폰의 주소록에서 모든 연락 가능한 전화 번호를 가져올 수있는 방법이 있지만 전화 번호 레이블을 가져 오는 방법이 있습니까? 예를 들면 다음과 같이 할 수 있습니다 : 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

감사합니다. –

+0

환영합니다. 기꺼이 도와 드리겠습니다 – shannoga

+3

(NSString *)을 사용하여 문자열 사용 (__bridge_transfer NSString *)으로 캐스트하는 대신. – Dev2rights

-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

무엇? 사용자가이 코드가 deutsch로 실행되는 경우 이메일 주소 – lensovet

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

이 아닌 전화 번호에 대해 질문 한 다음 "mobile"이 "mobil"으로 바뀝니다. . 그런 경우 어떻게 처리 할 수 ​​있습니까? 모든 문자열을 검사하지 못할 수도 있습니다. – BaSha

+0

확인. 원하는 언어로 프로젝트에 지역화 파일을 만듭니다. 이 파일에서 mobile = 'mpbiel'을 정의하십시오. 위의 코드에 따라 수정 중입니다. – abhi

+0

사실 내가 문제를 해결 했으므로 지역 레이블 대신 현지화 된 레이블을 비교해야합니다. locLabel : CFStringRef = ABMultiValueCopyLabelAtIndex (phones, numberIndex) .takeUnretainedValue() as CFStringRef; if (String (locLabel) == String (kABHomeLabel)) {} – BaSha

0

는 미리 정의 된 상수는 파일에 정의되어 kABPersonPhoneMobileLabel, kABPersonPhoneIPhoneLabel를 원하는 것을 할 수있다 ABPerson.h .

관련 문제