Search Posts in my Blog

Tuesday 6 March 2012

Reterive Data From the AddressBook



In this post we will learn on how to retrieve the data from the contact book, and in case you are new to the address book please refer to my earlier post and then continue with this post.

I got a request on how do I access the address book in the iPhone. So I took around 30 minutes to learn how and here is how we will do it. 
First create a new View-Based Application and call it addressBook.
Now the first thing we want to do is setup the 4 IBOutlets and the 1 IBAction for our project. We also need to include the headers for the address book framework. So open up addressBookViewController.h and you want to make it look like the following.
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface addressBookViewController : UIViewController  {
 IBOutlet UIButton *button;
 IBOutlet UILabel *firstName;
 IBOutlet UILabel *lastName;
 IBOutlet UILabel *number;
}

-(IBAction)getContact;

@end
Now that they are added we need to add in the framework files. So you will right click on the addressBook in theTargets option on the left panel and click on Get Info.
At the bottom there is a + and click that and you need to add the following items in AddressBook.framework andAddressBookUI.framework
Now lets layout our view. So double click on addressBookViewController.xib. You want your view to look like the following.
I used the —–’s just so you can see there is a UILabel there. Now we need to setup the connections for the 4 outlets and the one action. You want it to look like the following
So now lets open up addressBookViewController.m and we want to add in the following methods.
-(IBAction)getContact {
 // creating the picker
 ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
 // place the delegate of the picker to the controll
 picker.peoplePickerDelegate = self;

 // showing the picker
 [self presentModalViewController:picker animated:YES];
 // releasing
 [picker release];
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    // assigning control back to the main controller
 [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

 // setting the first name
    firstName.text = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

 // setting the last name
    lastName.text = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 

 // setting the number
 /*
  this function will set the first number it finds

  if you do not set a number for a contact it will probably
  crash
  */
 ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
 number.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);

 // remove the controller
    [self dismissModalViewControllerAnimated:YES];

    return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    return NO;
}

Now we can click build and go and see the following


I hope that this post was helpful for you 

No comments:

Post a Comment