Get a list of all contacts on iOS

G

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h>

CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

for(int i = 0; i < numberOfPeople; i++) {

ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSLog(@”Name:%@ %@”, firstName, lastName);

ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
NSLog(@”phone:%@”, phoneNumber);
}

NSLog(@”=============================================”);

}

About the author

mayur.bhansali
By mayur.bhansali

Category