How many entries are stored inside the iPhone adress book? - iphone

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.

Related

How to select multiple contacts from phone addressbook and record in the app?

I want to select multiple contacts from the iPhone address book and save selected contacts in my app. then I have to retrieve record of that selected contacts.
check this demo
1) multicontactsselector
2) knmultiitemselector
3) tkcontactsmultipicker
Try this
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
...
}

Get contacts without ABPeoplePickerNavigationController

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.

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...........

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.

Count total number of Phone Numbers in AddressBook iPhone

I am trying to get the total count for the phone numbers listed in the AddressBook, in all groups as a whole.
I can successfully retrieve Person count and Group count, just not the total number of Phone Numbers.
ABAddressBookRef m_addressbook = ABAddressBookCreate();
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
CFIndex nGroups = ABAddressBookGetGroupCount(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
Should return the count for everyone in the address book.
Try this:
ABAddressBookRef addressbook = ABAddressBookCreate();
int nPeople = ABAddressBookGetPersonCount(addressbook);
printf("%d",nPeople);