How to Access the Address Book for iOS Apps - dummies

How to Access the Address Book for iOS Apps

By Rajiv Ramnath

The iOS framework provides functions that enable your app to work with the databases that come with iOS devices. One such database is the Address Book, which includes people and their contact information. iOS devices also provide a Contacts app that allows users to manage this database by adding new contacts, updating them, deleting them, and so on.

The information in an Address Book is accessible to other apps, albeit in a controlled manner. For example, your app can present users with the contacts from the Address Book, allow them to select a contact, and then use the information of the selected contact. I sometimes use this functionality in Tic-Tac-Toe to gloat over my glorious Tic-Tac-Toe score via an e-mail to someone in my contact list.

To better understand, take a look at the Game Session view controller (TTTGameSessionViewController.m and .h) and follow along. First, you use the view controller to implement the ABPeoplePickerNavigationControllerDelegate protocol, as shown here:

@interface TTTGameSessionViewController:
         UIViewController
         <MFMailComposeViewControllerDelegate,
         MFMessageComposeViewControllerDelegate,
         ABPeoplePickerNavigationControllerDelegate,
         TTTCheckpointable> {
…
}

The method sendScoresByEmailWithContact (reproduced next) is the action method that initiates the process by presenting the Address Book picker and allowing the user to select the contact:

 - (IBAction) sendScoresByEmailWithContact:(id)sender{
  ABPeoplePickerNavigationController *picker =
   [[c alloc] init];
  picker.peoplePickerDelegate = self;
  [self presentViewController:picker animated:YES completion:nil];
 }

This method also sets its object (the Game Session view controller) as the delegate for the picker. When the picker finishes selecting an entry from the Address Book, the delegate method shouldContinueAfterSelectingPerson is called:

 - (BOOL)peoplePickerNavigationController:
   (ABPeoplePickerNavigationController *)peoplePicker
    shouldContinueAfterSelectingPerson:(ABRecordRef)selectedPerson {
  NSString* oneEmail=nil;
  ABMultiValueRef emails = ABRecordCopyValue(selectedPerson,
             kABPersonEmailProperty);
  if (ABMultiValueGetCount(emails) > 0) {
   oneEmail =
    (__bridge_transfer NSString*)
     ABMultiValueCopyValueAtIndex(emails, 0);
  }
  CFRelease(emails);
  [self dismissViewControllerAnimated:YES
    completion:^{[self sendScoresByEmailWithAddress:oneEmail];}];
  return NO;
 }

The e-mail address of the contact selected using the picker is extracted from the variable selectedPerson using the property kABPersonEmailProperty. Note also that the contact may have several e-mails, and only the first one (at index 0 in the extracted list) is used.

Next, the Picker view controller is dismissed using dismissViewControllerAnimated. Here comes the cool part: Setting the completion parameter to the method sendScoresByEmailWithAddress causes this method to be called right after the Address Book picker is dismissed. This process, in turn, causes the Mail app to fire up, with the address filled in, all ready for you to send your scores.

image0.jpg

Finally, look at the two methods bridge_transfer and CFRelease. They are necessary because the Address Book API isn’t integrated with the new Automated Reference Counting (ARC) memory management mechanism.