articles

Home / DeveloperSection / Articles / Fetching Device Contacts using ABAddressBook framework in iOS

Fetching Device Contacts using ABAddressBook framework in iOS

Tarun Kumar 3734 10-Dec-2015

ABAddressBook is a framework used for getting device contacts. Here we will learn some important codes that are playing very important role to fetch contacts with its complete information. Here we will see some very different type of codes like __bridge_transfer.  these types of codes are used to type cast C & C++ code according to Objective-C code.

ABAddressBook class also written in C/C++ code so if we will perform any type of action and use its return values into Objective-C classes like NSString, NSNumber, or any other class reference then it is mandatory that we type cast it into objective-C standard using bridge transfer.

In our next article we will create a sample using this framework.

Now, we will see some important points:

 We will start from the ABAddressBookRef class which will hold all the device contacts information. For getting device contacts use code like this:

ABAddressBookRef address = ABAddressBookCreateWithOptions(NULL,& error);

In this code we see a method ABAddressBookCreateWithOptions that is responsible for fetching contacts. 

 After that we will create a mutable array on which we will store all the contacts data that variable of ABAddressBookRef stores. Like this:

NSMutableArray *newTableData = [[NSMutableArray alloc] init];

NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(address);

After that we will create a for loop, it helps to fetch each person contact details, for loop will execute according the size of array. Like this: 

for(i = 0; i < [allContacts count]; i++)

  For holding the person contacts information we use reference of ABRecordRef class. Like this:

ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

   Now, for getting each person unique id we use ABRecordGetRecordID() method. This method gets reference of ABRecordRef as a parameter, this method will return number so we will assign it on reference of NSNumber, like this:    

NSNumber *contactId = [NSNumber numberWithInt:ABRecordGetRecordID(contactPerson)];


 

 For getting first name we use ABRecordCopyValue method, in this method we will pass two parameters, in first parameter will pass person reference and the second parameter we will pass first name property, like this:

NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);

For getting middle name we use ABRecordCopyValue method, in this method we will pass two parameters, in first parameter will pass person reference and the second parameter we will pass middle name property, like this: 

NSString *middleName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonMiddleNameProperty);

  For getting middle name we use ABRecordCopyValue method, in this method we will pass two parameters, in first parameter will pass person reference and the second parameter we will pass middle name property, like this:   

NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

For getting modification date we use ABRecordCopyValue method, in this method we will pass two parameters, in first parameter will pass person reference and the second parameter we will pass modification property, like this: 

NSDate *modificationDate = (__bridge_transfer NSDate *)ABRecordCopyValue(contactPerson, kABPersonModificationDateProperty);

   Now, for getting phone numbers we use a reference of ABMultiValueRef, because in device we stores different types of phone numbers for example; Home, iPhone, Mobile, work, main, and others. So first of all we get all the phone numbers in reference of ABMultiValueRef and after we will execute a for loop and in this loop we can get all the information about the phone numbers like number and its type. Like this:    

ABMultiValueRef numbersArr = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);

NSUInteger j = 0;
for(j = 0; j< ABMultiValueGetCount(numbersArr); j++) {
NSString *cellN = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(numbersArr, j); NSString *cellT =(__bridge NSString*)ABAddressBookCopyLocalizedLabel( ABMultiValueCopyLabelAtIndex(numbersArr, j)); }

  After executing all the necessary information from the reference of ABRecordRef we will release the memory, using this code:

CFRelease(address); 
 Next: we will learn how to create contact-sync application using
ABAddressBook framework : Sample on Contact-Sync in iOS

Updated 14-Dec-2017

Leave Comment

Comments

Liked By