Get contacts without ABPeoplePickerNavigationController - iphone

I Want to get the contacts on the device without using ABPeoplePickerNavigationController, it is possible in objective-c?

Yes, it's possible. Below there's a simple snippet of code that do the work:
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeopleRef = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, nil, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for( int i = 0 ; i < nPeople ; i++ ) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleRef, i );
CFStringRef tmpStringRef = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSLog(#"FirstName: %#", (NSString *)tmpStringRef);
CFRelease(tmpStringRef);
}
//clean up memory
CFRelease(allPeopleRef);
CFRelease(addressBook);
For more details, please refer to the Apple Developer Documentation here.

It is. Please refer to the AddresBook programming guide. Direct interaction section.

Related

Get iPhone's contacts list in Objective-C

I'd like to get my iPhone's contacts list without using the iOS Navigation Picker.
Here's the code I wrote...but it's not working, do you know why?
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef allPeopleRef = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, nil, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for(int i=0; i<nPeople; i++){
ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleRef, i );
CFStringRef tmpStringRef = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSLog(#"FirstName: %#", (__bridge NSString *)tmpStringRef);
CFRelease(tmpStringRef);
}
CFRelease(allPeopleRef);
CFRelease(addressBook);

How many entries are stored inside the iPhone adress book?

Is it possible to know the number of entries stored inside the iOS address book programmatically?
Use ABAddressBookGetPersonCount to count total contacts, Like :-
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
...
}
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex peopleCount = CFArrayGetCount(people);
CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(addressBook);
CFIndex groupsCount = CFArrayGetCount(groups);
For more information please see the Address Book Programming Guide for iOS.

Get addressbook group for particular contact

In my app i am getting contacts like this..
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for( int i=0;i< nPeople;i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
.....
and so on
}
can anyone suggest me how to get the group for nPeople.
Check the following documentation
http://developer.apple.com/library/mac/#documentation/UserExperience/Reference/AddressBook/Classes/ABPerson_Class/Reference/Reference.html#//apple_ref/occ/instm/ABPerson/parentGroups
There is a method parentGroups which returns the array of groups to which the person belongs...........

How to use ABAddressBookRef for iPhone application?

I want reference of my address book in one of iPhone application. I have used below code. But its showing [people count] = 0.
I have 3-4 contacts on my addressbook application still [people count] returns zero.
Is anything more, I am missing here?
Code is:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSMutableArray *people = [[[(NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook) autorelease] mutableCopy]autorelease];
[people sortUsingFunction:(int (*)(id,id,void*)) ABPersonComparePeopleByName context:(void*)ABPersonGetSortOrdering()];
NSLog(#"count..%i",[people count]);
for(int i = 0 ; i < [people count]; i++){
ABRecordRef person = [people objectAtIndex:i];
//get phone no fill phone no into array
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
if(multi!=NULL && ABMultiValueGetCount(multi)>0) {
NSLog(#"in value..");
}
else {
NSLog(#"no value");
}
}
Thank You
try that -
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people;
people = ABAddressBookCopyArrayOfAllPeople(HiByeGroup);
if (people){
CFArrayCreateMutableCopy(kCFAllocatorDefault,CFArrayGetCount(people),people);
NSInteger numberOfPersonsInAB=CFArrayGetCount(peopleMutable);
for (int i =0; i< numberOfPersonsInAB; i++) {
//your code
}
CFRelease(people);
UPdate
As Jamon Holmgren noted, this answer is old and ABAddressBookCreate() is deprecated in iOS6. You need to use ABAddressBookCreateWithOptions() instead.
GOOD LUCK
Just follow given code over here. A perfect example to interact with ABAddressBook.

Why I'm getting “EXC_BAD_ACCESS” upon trying to release?

Would you please have a look at me code:
(void)loadContactsFromAddressBook {
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex count = ABAddressBookGetPersonCount(addressBook);
ABRecordRef person;
for (int i = 0; i < count; i++) {
person = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef cfStr = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* firstName = (NSString*) cfStr;
[contacts addObject:firstName];
CFRelease(cfStr);
}
CFRelease(addressBook);
CFRelease(allPeople);
}
I tried to CFRelease(person) after the for block but the application crashes with “EXC_BAD_ACCESS”. If I remove CFRelease(person), the application works.
You didn't create the person, you only referenced it. Insert child support joke here.