O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.3. Retrieving All the People in the Address Book

Problem

You want to retrieve all the contacts in the user’s address book.

Solution

Use the ABAddressBookCopyArrayOfAllPeople function to retrieve an array of all contacts:

- (void) readFromAddressBook:(ABAddressBookRef)paramAddressBook{
    
    NSArray *arrayOfAllPeople = (__bridge_transfer NSArray *)
    ABAddressBookCopyArrayOfAllPeople(paramAddressBook);
    
    NSUInteger peopleCounter = 0;
    for (peopleCounter = 0;
         peopleCounter < [arrayOfAllPeople count];
         peopleCounter++){
        
        ABRecordRef thisPerson =
        (__bridge ABRecordRef)
        [arrayOfAllPeople objectAtIndex:peopleCounter];
        
        NSLog(@"%@", thisPerson);
        
        /* Use the [thisPerson] address book record */
    }
    
}

Discussion

After accessing the user’s address book database, we can call the ABAddressBookCopyArrayOfAllPeople function to retrieve an array of all the contacts in that address book. The return value of this function is an immutable array of type CFArrayRef. You can’t work with this type of array as you would work with instances of NSArray, but you have two ways to traverse a CFArrayRef array. First, it natively supports two functions:

CFArrayGetCount

Gets the number of items in an instance of CFArrayRef. This is similar to the count instance method of an NSArray.

CFArrayGetValueAtIndex

Retrieves an item at a specific location of an instance of CFArrayRef. This is similar to the objectAtIndex: instance method of an NSArray.

Second, the CFArrayRef Core Foundation object is one of the objects that supports toll-free bridging ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required