Display Address Book in iPhone

In this post we will learn on how to work with the address book framework.
This post is divided into three parts, which deals with the following subtopics
a.    Show the address book

So let’s began with our first part and that would be just to display the address book. 
Design Phase: To display the Address book we will have a button on our view and on the hit of the button we will display the address book.



Step 1: Open Xcode and create a windows based application and name it the Address book and then add the UIViewController subclass file into it with the name ShowAddressBook, this will result in the adding of the two new files into your project that’s the ShowAddressBook.h and ShowAddressBook.m. After completing this I want you to add a framework into your project that’s the

  
Step 2: Select the ShowAddressBook.h file and add this piece of code

#import <UIKit/UIKit.h>

#import <AddressBookUI/AddressBookUI.h>


@interface ShowAddressbook : UIViewController{
 ABPeoplePickerNavigationController *peoplePicker;
}
-(void)displayContact;

@end
Code Explanation: In the above code you can see that I have imported the framework. The ABPeoplePickerNavigationController class (whose instances are known as people-picker navigation controllers) implements a view controller that manages a set of views that allow the user to select a contact or one of its contact-information items from an address book. And finally we have a function that will display the address book.
Step 3: Select the ShowAddressBook.m file and add this piece of code inside the init method
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self == [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization


           peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
           peoplePicker.peoplePickerDelegate = self;
           self.title = @"Display Contact bar ";
    }
    return self;
}
Code Explanation: In the above code I have initialized memory for the people picker controller and set the delegate property to self we will talk about the delegate method of the ABPeoplePickerNavigationController later
Remember the function that we have declared inside the ShowAddressBook.h file (displayContact) its time to give body to that function.

-(void)displayContact
{

    [self presentModalViewController:peoplePicker animated:YES];
}
Code Explanation: In the above code I have made the people picker as the modal view that’s all.
The delegate method of the people picker controller is given below with the code.
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
{
         [self dismissModalViewControllerAnimated:YES];
}
Code Explanation: The above delegate method gets called when the cancel button of the people picker is touched and on its touch I have dismissed the modal view (people picker).
Step 4: Select the app delegate.m file and add this ShowAddressBook view to the iPhone window here’s the code that will help you to do that
#import "AddressBookAppDelegate.h"
#import "ShowAddressbook.h"

@implementation AddressBookAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {   

    // Override point for customization after application launch
        ShowAddressbook *showVC = [[ShowAddressbook alloc]init];

       [window addSubview: showVC.view];
       [window makeKeyAndVisible];
}
Step 5: Run the application and hit the button to get the view which would be something like this

output

i hope that this post has helped you in learning on how to display the contact list in the iPhone, Happy iCoding and have a great Day. 

Comments

  1. Thanks for this one.


    Sir , i have a problem regarding to ABPeoplePickerNavigationController , i want to only select it(by tick mark) nothing further action. and wanted to take only mobile number of that contact.


    Please help me.

    Thanks

    ReplyDelete
  2. Sir this is a very useful tutorial for accessing address book.

    ReplyDelete

Post a Comment