Search Posts in my Blog

Showing posts with label Retrieve the Data from the address book. Show all posts
Showing posts with label Retrieve the Data from the address book. Show all posts

Tuesday, 6 March 2012

Add Data inside the address Book


In this post we will learn on how to save the data inside the contact book via code,

Note: If you are new to the address book then it is preferred that you must read my earlier two post on the address book framework, the link to those are given below:
 Design Phase: Here’s a view at the final output, you have to create a view which looks something like the one given in the image given below


 Step 1: Open Xcode and create a windows based application and add the UIViewController subclass file to your project with the name AddContactViewController, now this will lead in adding of two new files into your project with the name AddContactViewController.h and AddContactViewController.m. Make very sure that you have added the AddressBookUI.framework into your project. Once this is done then kindly create the view just like the one in the above image.
Step 2: Inside the AddContactViewController.h create three functions that will be associated with the buttons
 -(void) addcontactToAddressBook;
-(void) displayContactBook;
-(void) clearRecords;
The first function addcontactToAddressBook will add the data present in the textfields into the addressbook and that data will appear in the addressbook of your device or simulator. You must be familiar with the second function all it does is displays the address book. The last function that’s clearRecords will clear all the record from the textfields.
Given below is the displayContactBook function which we have seen earlier
-(void) displayContactBook
{
    ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentModalViewController:peoplePicker animated:YES];
}
and the delegate method
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
{
    [self dismissModalViewControllerAnimated:YES];
}
The clearRecords function body
-(IBAction) clearRecords
{
    firstname.text =@"";
    lastname.text =@"";
    companyname.text=@"";
    email.text =@"";
    mobileno.text =@"";
}
Step 3: The code for the addcontactToAddressBook function
-(void) addcontactToAddressBook
{
    CFErrorRef anyerror = NULL;

     //point to iPhone address book
    ABAddressBookRef iphoneaddressBook = ABAddressBookCreate();
   
    //create a new person record
   
    //ABAddressBookRef object is a reference to the object of the address book data base
   
    ABAddressBookRef newPerson = ABPersonCreate();
   
    //save some data like first name and last name
   
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty,firstname.text, &anyerror);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty,lastname.text, &anyerror);
    ABRecordSetValue(newPerson, kABPersonOrganizationProperty,companyname.text, &anyerror);
   
    //KABPersonFirstNameProperty are constants defined by apple
    //ABMultiValueRef is a reference to multi value property
    //ABMultiValueCreateMutable(<#ABPropertyType type#>) returns a empty mutable multivalue property.

//This should only be used when multiple records for a single attribute is about to be entered like email address or phone numbers

ABMultiValueRef multiPhone =ABMultiValueCreateMutable(kABMultiStringPropertyType);

ABMultiValueAddValueAndLabel(multiPhone, mobileno.text,kABPersonPhoneMobileLabelNULL);

ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &anyerror);

CFRelease(multiPhone);
    //adding the email to the iPhone contact

ABMultiValueRef emailiphone =ABMultiValueCreateMutable(kABMultiStringPropertyType);

ABMultiValueAddValueAndLabel(emailiphone, email.textkABWorkLabel,NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, emailiphone, &anyerror);

CFRelease(emailiphone);
   
    //adding the entire record to the address book
    ABAddressBookAddRecord(iphoneaddressBook, newPerson, &anyerror);
    ABAddressBookSave(iphoneaddressBook, &anyerror);
   
}

Step 4: Add this view to the iPhone window by selecting the appdelegate.m file of your project.
#import "AddressBookAppDelegate.h"
#import "addmycontact.h"

@implementation AddressBookAppDelegate

@synthesize window;

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

    // Override point for customization after application launch
    addmycontact *addVC = [[addmycontact alloc]init];
    [window addSubview: addVC.view];
    [window makeKeyAndVisible];
}
Step 5: Run the application and add the values inside the text field and then press the add button after that touch the display contact book button and see the magic. :)
I hope that this post was helpful to you

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 

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 theABPeoplePickerNavigationController 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