2013-05-17 51 views
4

我在我的IOS應用程序中使用Addressbook來獲取聯繫人姓名和號碼,但發生了一些奇怪的事情,它顯示了一些聯繫人的電話號碼。IOS地址簿沒有從聯繫人列表中獲取所有電話號碼

我希望它會顯示與電話號碼的所有聯繫人列表。

所以這裏是我的代碼獲取電話號碼:

-(void)getAddressbookData 
{ 
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000 
     ABAddressBookRef addressBook = ABAddressBookCreate(); 
    #else 
     CFErrorRef *error = nil; 
     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error); 
    #endif 
     NSArray * people; 
     BOOL accessGranted = [self __addressBookAccessStatus:addressBook]; 

     if (accessGranted) 
     { 
      people = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
      // Do whatever you need with thePeople... 
     } 
     CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 
     NSMutableArray *contactArray = [[NSMutableArray alloc] init]; 

     for (CFIndex i = 0; i < nPeople; i++) 
     { 
      ABRecordRef record = CFArrayGetValueAtIndex((__bridge CFArrayRef)(people), i); 
      NSString *firstName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty); 
      NSString *lastName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty); 
      NSString *fullName = nil; 

      if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst) 
       fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
      else 
       fullName = [NSString stringWithFormat:@"%@, %@", lastName, firstName]; 

      [contactArray addObject:fullName]; 

      // 
      // Phone Numbers 
      // 
      ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty); 
      CFIndex phoneNumberCount = ABMultiValueGetCount(phoneNumbers); 

      NSMutableArray *numbersArray = [[NSMutableArray alloc] init]; 
      for (CFIndex k=0; k<phoneNumberCount; k++) 
      { 
       CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, k); 
       CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex(phoneNumbers, k); 
       CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel(phoneNumberLabel); 
       // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile" 

       // Find the ones you want here 
       // 
       // NSLog(@"-----PHONE ENTRY -> name:%@ : %@ : %@", fullName, phoneNumberLocalizedLabel, phoneNumberValue); 
       [numbersArray addObject:CFBridgingRelease(phoneNumberValue)]; 

       CFRelease(phoneNumberLocalizedLabel); 
       CFRelease(phoneNumberLabel); 
       CFRelease(phoneNumberValue); 
      } 

      // NSLog(@"phone numbers %@", numbersArray); 
      [contactDictionary setObject:numbersArray forKey:fullName]; 

      CFRelease(record); 

     } 

     selectContacts = contactArray; 
     // NSLog(@"dictionary of array %@", contactDictionary); 

     //NSLog(@"contacts count %d", [selectContacts count]); 
    } 

    -(BOOL)__addressBookAccessStatus:(ABAddressBookRef) addressBook 
    { 
     __block BOOL accessGranted = NO; 

     if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6 
      dispatch_semaphore_t sema = dispatch_semaphore_create(0); 

      ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { 
       accessGranted = granted; 
       dispatch_semaphore_signal(sema); 
      }); 

      dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 
      // dispatch_release(sema); 
     } 
     else { // we're on iOS 5 or older 
      accessGranted = YES; 
     } 
     return accessGranted; 
    } 

所以numbersArray是空白的一些我不知道爲什麼它正在發生的接觸。

回答

5

試試這個電話號碼。

ABAddressBookRef addressBook = ABAddressBookCreate(); 
    NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
    for(id person in people){ 
    //fetch multiple phone nos. 
     ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty); 
     for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) { 
      NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(multi, j); 
      [numbersArray addObject:phone]; 
      [phone release]; 
     } 
    } 

,你必須使用之前的Alloc您的陣列。在viewDidLoad方法中寫入此分配數組

  numbersArray=[[NSMutableArray alloc] init]; 
+0

感謝這有助於 –

+0

歡迎哥們...... –

+0

這是很好的,你是釋放'phone',但你不釋放'multi'或'people'或'addressBook'。通過靜態分析器運行它,它會指出泄漏。如果你用'CFBridgingRelease'替換'(NSArray *)'和'(NSString *)'的轉換,那麼ARC將處理這個。 (或者如果你使用'__bridging_transfer',就像OP一樣,也可以完成同樣的事情。)不用說,在iOS 9中,'Contacts'框架完全消除了這個問題,但顯然這個問題早於該框架。 – Rob

1

我正在使用此代碼來訪問我的地址簿中的手機號碼,它的工作正常。

ABAddressBookRef addressBook = ABAddressBookCreate(); 
CFArrayRef allSources = ABAddressBookCopyArrayOfAllPeople(addressBook); 
for (CFIndex i = 0; i < ABAddressBookGetPersonCount(addressBook); i++) 
    { 
ABRecordRef aSource = CFArrayGetValueAtIndex(allSources,i);  
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty); 
     NSString* mobileLabel; 

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

      mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i); 

      if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) 
      { 
       home_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i) retain]; 

      } 

      if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) 
      { 
       basic_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i)retain]; 


      } 
      if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMainLabel]) 
      { 
       work_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i)retain]; 

      } 
     } 

您可以試試這個。

+0

因爲你抱怨說,你沒有得到一些用戶的電話號碼只有然後檢查你是否有電話號碼這些聯繫人在您的地址簿 –

+0

中感謝它的運作良好。仍然需要根據ios版本進行更改。 –

1

這是@ Samir的答案的Swift版本爲我工作。

var allNumbers: [AnyObject] = [] 
let adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue() 
let people = ABAddressBookCopyArrayOfAllPeople(adbk).takeRetainedValue() as NSArray as [ABRecord] 
for person in people { 
    var phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue() 
    for j in 0..<ABMultiValueGetCount(phones) { 
     var phone: String = ABMultiValueCopyValueAtIndex(phones, j).takeRetainedValue() as String 
     allNumbers.append(phone) 
    } 
} 
println(allNumbers) 
+0

什麼是var adbk? –

相關問題