Reputation: 11
Hi, I am using address book in my application. I get contacts in simulator. But in iPhone 4S it does not show any contacts in device. And in iPhone 4 I get only 1 contact. I don't understand why it not show all contacts. I am using following code. Please help me.
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* phoneNumber=@"";
NSString *mobileLabel=@"";
//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
{
phoneNumber=@"";
}
NSLog(@"%@",phoneNumber);
}
Upvotes: 1
Views: 863
Reputation: 645
Did you ask for access to the contacts on the iPhone?
I experienced that on the simulator, it is not needed to ask that (all the contacts are loaded), but on the device, for those functions to work, you must ask the user to give you access to his contacts.
You can try :
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
}
And then, your code should work!
Upvotes: 1
Reputation: 5242
Try this one:
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);
}
Don't forget to CFRelease the object you copy.
Upvotes: 0