Saturday, January 28, 2012

Get Data from address book without displaying the address book modal controller

At times it might be required that you have to get only the data from your contact list but should not display the address book modal view controller, so in todays post we are going to see something like this. (For earlier data on address book framework kindly refer to my earlier blog post here).


Step 1: Create an empty xcode project and create a view which looks like the one given below. The gray portion consists of the table view controller so don't forget to add the datasource method of the table view controller which are given below:


1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath









Also connect the datasource and delegate property to the file owner if you are using the interface builder or set the property to self if you are coding this view.  Once done add the address book framework into your project and import it into the .h file of the AddressBookNamesViewController


Step 2: In the .h file of the AddressBookNamesViewController declare the function and an array, in the array we shall retrive the names of the person inside your address book given below is the code for the same
















Step 3: Select the dot m file and now in the .m file we will code for the function given below is the code for the same.
















Don't forget to connect the function with the buttons touch up inside method also i have given memory to the mutable array in the init method of the AddressBookNamesViewController.m file


Step 4: Its time to code for the datasource method of the table view


























Step 5: Select the app delegate.m file and add the AddressBookNamesViewController view into the iPhone window but make sure to import the AddressBookNamesViewController.h file into the app delegate.m file before creating its object.
















Finally run the application and hit the button to see the names o fall your contact as a list in the table view.






You can download the code from here.


Happy iCoding and have a great day.



4 comments:

  1. ain't u have tutorials on sqlite?

    ReplyDelete
  2. @Anks: Its better to use core data than sqlite, but let me see what i can do on sqlite havent used it much,

    ReplyDelete
  3. Sir I'm making a birthday reminder in which I have a need to store contacts in array so that it is stored in array[0],array[1] and so on and I can use it for table view and compare it with other array.

    ReplyDelete