How can I get all contacts from a specific group in the address book?

Hi, I have several groups in my iPhone address book that contains several contacts. For instance:

iPhone Address Book, Group 1, Group 2, etc.

Each group contains contact information, such as first name, last name, email address, phone number. Now, having selected any group, I should get all the details of the added contacts. Can someone please direct me, how can I get all contact details from a specific group?

Please pay attention to some recommendations.

+6
source share
2 answers
CFErrorRef error = NULL; ABAddressBookRef addrBook = ABAddressBookCreate(); CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(addrBook); CFIndex numGroups = CFArrayGetCount(groups); for(CFIndex idx=0; idx<numGroups; ++idx) { ABRecordRef groupItem = CFArrayGetValueAtIndex(groups, idx); CFArrayRef members = ABGroupCopyArrayOfAllMembers(groupRef); if(members) { NSUInteger count = CFArrayGetCount(members); for(NSUInteger idx=0; idx<count; ++idx) { ABRecordRef person = CFArrayGetValueAtIndex(members, idx); // your code } CFRelease(members); } } CFRelease(groups); CFRelease(addrBook); 

This code does not guarantee tightness, so double check it. This is more or less correct.

+10
source

Everything is explained in the documentation, so please tell us what you do not understand in it. What have you tried? What did you get, what mistakes did you have?

If you want to work with contacts, in addition to the very comprehensive Address Book Programming Guide , you, of course, Address Book Reference and especially ABGroup Reference Documentation for working with groups. And the latter explicitly contains a method for getting all members of the group . Therefore, you should have everything you need.

 CFArrayRef cfmembers = ABGroupCopyArrayOfAllMembers(group); NSArray* members = (NSArray*)cfmembers; // working with NSArray is usually easier that CFArrays so I like using toll-free bridging for(ABRecordRef person in members) { // ... your code ... } CFBridgingRelease(cfmembers); // release memory when done, following the usual memory mgmt rules 
+2
source

Source: https://habr.com/ru/post/926391/


All Articles