2012-04-30 54 views
1

我想要爲iPhone地址簿中存儲的聯繫人獲取帶有「iPhone」,「家庭電話」,「手機號碼」,「其他號碼」等差異標籤的所有電話號碼。如何獲取地址簿中所有聯繫人的所有電話號碼和標籤?

我該如何得到它?

請幫忙。

在此先感謝。

我想:這是崩潰

ABAddressBookRef ab=ABAddressBookCreate(); 

    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(ab); 
    NSMutableArray *allNumbers = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(people)]; 

    for (CFIndex i = 0; i < CFArrayGetCount(people); i++) { 
     ABRecordRef person = CFArrayGetValueAtIndex(people, i); 
     ABMultiValueRef numbers = ABRecordCopyValue(person, kABPersonPhoneProperty); 

     for (CFIndex j=0; j < ABMultiValueGetCount(numbers); j++) { 


      CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(numbers, i); 
      NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 

      CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(numbers, j); 
      CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(numbers, j); 
      NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 

      NSLog(@" ####### phone no -> %@ , phone label -> %@ #######)", locLabel1, phoneLabel1); 
      //CFRelease(phones); 
      NSString *phoneNumber = (NSString *)phoneNumberRef; 
      CFRelease(phoneNumberRef); 
      CFRelease(locLabel); 
      NSLog(@"phone no -> %@ , phone label -> %@)", phoneNumber, phoneLabel); 
      [phoneNumber release]; 
     } 
     CFRelease(numbers); 
    } 

    CFRelease(people); 

回答

1

解決這樣說:

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++) 
    { 

     CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(phones, j); 
     NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel1); 

      NSLog(@" ### %@ --- %@ ###)", locLabel1, phoneLabel1); 

    } 
}  
15

嘗試:

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); 

    NSString *phoneNumber = (NSString *)phoneNumberRef; 
    CFRelease(phoneNumberRef); 
    CFRelease(locLabel); 
    NSLog(@" - %@ (%@)", phoneNumber, phoneLabel); 

    [phoneNumber release]; 
} 
+0

是不是有'CFRelease(電話);'後失蹤的for循環? – newenglander

+0

「ABMultiValueRef *手機」應更改爲「ABMultiValueRef手機」 – glishijie

0

也得到特別記錄的ID號碼,做這個:

ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook); 

     ABRecordRef ref = CFArrayGetValueAtIndex(all, indexPath.row); 
     NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
     NSLog(@"Name %@", firstName); 

     NSInteger myID = ABRecordGetRecordID(ref); 
     NSLog(@"Record id is > %d", myID); 

     ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook,myID); 

     ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); 

     for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) 
     { 

      NSString* num = (NSString*)ABMultiValueCopyValueAtIndex(phones, j); 

      CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(phones, j); 

      NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel1); 

      NSLog(@"%@ --- %@)", num, phoneLabel1); 

     } 
2

這是固體的ARC 64位iOS8上:

- (NSArray *)phoneNumbersOfContactAsStrings:(ABRecordRef)contactRef { 

NSMutableArray *mobilePhones = [NSMutableArray arrayWithCapacity:0]; 

ABMultiValueRef phones = ABRecordCopyValue(contactRef, kABPersonPhoneProperty); 
NSArray *allPhoneNumbers = (NSArray *)CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones)); 

for (NSUInteger i=0; i < [allPhoneNumbers count]; i++) { 
    if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) { 
     [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))]; 
    } 
    if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel]) { 
     [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))]; 
    } 
} 

CFRelease(phones); 
return mobilePhones; 
} 
相關問題