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.
Design Phase: In this demo we shall retrieve some of the values from the address book into our UI, so please prepare a view which looks like something like this
Step 1: Open Xcode and create a windows based application and add the UIViewController subclass file to your project with the name ReteriveSingleData, now this will lead in adding of two new files into your project with the name ReteriveSingleData.h and ReteriveSingleData.m. Make very sure that you have added the AddressBook.framework and the AddressBookUI.framework into your project
Step 2: Please make a view like the one that is shown above and then inside the ReteriveSingleData.h file import the frameworks that you have added
#import <AddressBookUI/AddressBookUI.h>
Also add a function inside the ReteriveSingleData.h file on the hit of which we will be displaying the address book UI to the user. Its time to code for its body
-(void) getContactData
{
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
peoplePicker.peoplePickerDelegate= self;
[self presentModalViewController:peoplePicker animated:YES];
[peoplePicker release];
}
Code Explanation: In the above code I have initialized memory for the people picker controller and set the delegate property to self.
The people picker controller has its delegate method which gets called when a particular value from the people picker is selected and this is the delegate method
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
here’s the code written inside the body of the delegate method that will help us to pick up the selected value from the address book
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
firstname.text = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastname.text = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
//the contact number of a person can be more than one so ABMultiValue is used
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
pnumber.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
[self dismissModalViewControllerAnimated:YES]; return YES;
}
Code Explanation: The ABRecordCopyValue is a function which accepts two parameter the first parameter is for the record whose value you want to obtain and the second is the property name in record whose value you wish to obtain these two are different things let me give you an example for this
Example: Let’s say inside the database I am a record whose value you want to obtain and so my property name would be name, phone number, address, dob, email etc.
Step 3: Also we have to implement one more delegate method of the ABPeoplePickerController and that delegate method is given below
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
{
[self dismissModalViewControllerAnimated:YES];
}
we implemented this method because on the hit of the cancel we have to remove the people picker from the main view.
Step 4: Select the AppDelegate.m file and add the ReteriveSingleData view to the iPhone window here’s the code to do that
#import "AddressBookAppDelegate.h"
#import "RetriveSingleData.h"
@implementation AddressBookAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
RetriveSingleData *retrieveVC = [[RetriveSingleData alloc]init];
[window addSubview: retrieveVC.view];
[window makeKeyAndVisible];
}
Step 5: Run the application, to see the output, which would look some what like this
Output |
Selected Record |
I hope that this post was helpful for you in learning on how to get the data from the address book inside your application, Happy iCoding and have a great Day.
can u provide a tutorial to display all contacts in addressbook in tableview on a button click without displaying the addressbook picker view
ReplyDelete@Anks: On your request here is the tutorial for you:
ReplyDeletehttp://iphonebyradix.blogspot.com/2012/01/get-data-from-address-book-without.html