Get addressbook group for particular contact - iphone

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

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

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.

How can i get the configured Emails in iPhone

I need to get the user emails those are configured in iPhone using cocoa touch. How can i do that .
Thank you for answers .
This code will fetch all contacts in the address book and output the email addresses associated with them:
// get a list of all contacts in the address book db
ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(ab);
CFIndex nPeople = ABAddressBookGetPersonCount(ab);
for( int i = 0; i < nPeople; ++i ) {
ABRecordRef ref = CFArrayGetValueAtIndex(contacts,i);
// output the contact's name
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString *contactFirstLast = [NSString stringWithFormat:#"%#,%#:",
firstName, lastName];
NSLog(#"%#", contactFirstLast);
// output all email addresses stored for the contact
ABMultiValueRef emailRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
CFIndex nEmails = ABMultiValueGetCount(emailRef);
CFArrayRef emails = ABMultiValueCopyArrayOfAllValues(emailRef);
for( int j = 0; j < nEmails; ++j ) {
CFStringRef email = CFArrayGetValueAtIndex(emails, j);
NSLog(#"\t%#", email);
};
// clean up
CFRelease(emailRef);
CFRelease(firstName);
CFRelease(lastName);
}
CFRelease(contacts);