2012-05-30 85 views
1

嗨,我在我的應用程序中使用地址簿。我在模擬器中獲取聯繫人。但在iPhone 4S中,它不顯示設備中的任何聯繫人。而在iPhone 4中,我只有1個聯繫人。我不明白爲什麼它不顯示所有聯繫人。我正在使用以下代碼。請幫幫我。iphone中獲取聯繫人信息的問題

ABAddressBookRef ab=ABAddressBookCreate(); 
NSArray *arrTemp=(NSArray *)ABAddressBookCopyArrayOfAllPeople(ab); 
UIImage* image; 
ContactArray=[[NSMutableArray alloc] init]; 
for (int i=0;i<[arrTemp count];i++) 
{ 
    NSMutableDictionary *dicContact=[[NSMutableDictionary alloc] init]; 
    NSString *str=(NSString *) ABRecordCopyValue([arrTemp objectAtIndex:i], kABPersonFirstNameProperty); 
    UIImage *imgContactImage=(UIImage *)ABPersonCopyImageDataWithFormat([arrTemp objectAtIndex:i],kABPersonImageFormatOriginalSize); 

    if(ABPersonHasImageData([arrTemp objectAtIndex:i])){ 
     image = [UIImage imageWithData:(NSData *)ABPersonCopyImageData([arrTemp objectAtIndex:i])]; 
    } else { 
     image = [UIImage imageNamed:@""]; 
    } 
    NSString* [email protected]""; 

    NSString *[email protected]""; 

    //Get mobile phone number 
    ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue([arrTemp objectAtIndex:i], kABPersonPhoneProperty); 
    CFRelease(phoneNumbers); 

    if(ABMultiValueGetCount(phoneNumbers)>0) { 
     for(CFIndex j = 0; j < ABMultiValueGetCount(phoneNumbers); j++) { 
      mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phoneNumbers, j); 
      if([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]) { 

     phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, j); 
      } 
     } 
    } 
    else 
    { 
     [email protected]""; 
    } 

    NSLog(@"%@",phoneNumber); 
} 
+0

試試這些代碼http://stackoverflow.com/questions/4883248/load-all-the-contacts-from-iphone-phonebook-in-a-variable和http://zcentric.com/2008/09/ 19/access-the-address-book /希望這會有所幫助.. –

回答

0

試試這個:

ABAddressBookRef addressBook = ABAddressBookCreate(); 
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 

for (int i = 0; i < nPeople; i++) { 
    // Get Contact info 
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 
    ABRecordID recordID = ABRecordGetRecordID(person); 

    NSString *firstName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
    NSString *lastName = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty); 
} 

不要忘了CFRelease複製的對象。

1

您是否要求訪問iPhone上的聯繫人?

我在模擬器上遇到過這種情況,不需要詢問(所有聯繫人都已加載),但在設備上,要使這些功能起作用,您必須要求用戶允許您訪問其聯繫人。

你可以試試:

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL); 

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { 
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { 
     // First time access has been granted, do what you want 
    }); 
} 
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { 
    // The user has previously given access, do what you want 
} 
else { 
    // The user denied access, ask him to reactive them on the settings 
} 

然後,你的代碼應該工作!