Monday, July 4, 2011

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, kABPersonPhoneMobileLabel, NULL);

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

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


ABMultiValueRef emailiphone = ABMultiValueCreateMutable(kABMultiStringPropertyType);

ABMultiValueAddValueAndLabel(emailiphone, email.text, kABWorkLabel, 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.




I hope that this post was helpful to you in learning how to add the data inside the address book, until then happy iCoding and have a great Day. 

No comments:

Post a Comment