Tuesday 5 June 2012

How to create sample iPhone app

  • Open X code 4.3.2
  • click on "create X code new Project
  • select empty based application(Window Based Application)
  • select next and give the name as "HelloWorldApp"
  • select device family as iPhone.

click on next then create an Application.

  • Now run the application(Command + R) . It'll shows an empty window on simulator.

                                                       ----------------------------------------
  • Now we'll create TabBar on Window. The IOS application starts with main method and Did finish launching method so go to AppDelegate.m and create an Object of TabBar.
           UITabBarController *tab = [[UITabBarController alloc]init];
  • Now we'll add the Object to Window.
                               [self.window addSubview:tab.view];
  • Now run the application it will show TabBar on window.




Now we are going to add three tabs on TabBar  (Services,ContactUs,Get a Quote) .
Add the class and give name as "ServicesViewController".
  • Right click on AppDelegate.m then select new file , select Objective C class, Click "Next" button, give the name as "ServicesViewController" and click on "Xib for user interface", Goto Next, then Create. It'll creates ServicesViewController.h, ServicesViewController.m, ServicesViewController.xib.

Select Objective C class then go next

Give name as ServicesViewController

Now click on Create


It'll add ServicesViewController.h, .m, .Xib.


  • Go to AppDelegate.m and add #import "ServicesViewController.h"
  • Create a ServicesViewController class object in Did Finish Launching method        
     UITabBarController *tab = [[UITabBarController alloc]init];

     ServicesViewController *services = [[ServicesViewController
     alloc]initWithNibName:@"ServicesViewController" bundle:nil];

  • We should give the title on Tab so write the code below.
            UITabBarController *tab = [[UITabBarController alloc]init];
     ServicesViewController *services = [[ServicesViewController
     alloc]initWithNibName:@"ServicesViewController" bundle:nil];
     services.title = @"SERVICES";
  • Take  NSArray class and assign the "service" object to it.
            UITabBarController *tab = [[UITabBarController alloc]init];
     ServicesViewController *services = [[ServicesViewController
     alloc]initWithNibName:@"ServicesViewController" bundle:nil];
     services.title = @"SERVICES";
     NSArray *arrNames= [NSArray arrayWithObjects:services, nil];
     [tab setViewControllers:arrNames];
  • Now run the Appllication. It'll shows services Tab.

  • Add the class and give name as "ContactViewController" as above Procedure.
  • Right click on AppDelegate.m then select new file , select Objective C class, Click "Next" button, give the name as "ContactViewController" and click on "Xib for user interface", Goto Next, then Create. It'll creates ContactViewController.h, ContactViewController.m, ContactViewController.xib.

  • Go to AppDelegate.m and add #import "ContactViewController.h"
  • Create a ContactViewController class object in Did Finish Launching method
          UITabBarController *tab = [[UITabBarController alloc]init];
    ServicesViewController *services = [[ServicesViewController
    alloc]initWithNibName:@"ServicesViewController" bundle:nil];
    ContactViewController *contact = [[ContactViewController 
    alloc]initWithNibName:@"ContactViewController" bundle:nil];

  • We should give the title on Tab so write the code below.
          UITabBarController *tab = [[UITabBarController alloc]init];
    ServicesViewController *services = [[ServicesViewController 
    alloc]initWithNibName:@"ServicesViewController" bundle:nil];
    services.title = @"SERVICES";

    ContactViewController *contact = [[ContactViewController 
    alloc]initWithNibName:@"ContactViewController" bundle:nil];
    contact.title = @"ContactUs";
  • Add contact object to NSArray Class
          UITabBarController *tab = [[UITabBarController alloc]init];
    ServicesViewController *services = [[ServicesViewController
    alloc]initWithNibName:@"ServicesViewController" bundle:nil];
    services.title = @"SERVICES";
    ContactViewController *contact = [[ContactViewController
    alloc]initWithNibName:@"ContactViewController" bundle:nil];
    contact.title = @"ContactUs";
    NSArray *arrNames= [NSArray arrayWithObjects:services, contact,
    nil];
    [tab setViewControllers:arrNames];

  • Now run the Appllication. It'll shows services Tab and ContactUs Tab.

  • Add the class and give name that "GetaQuoteViewController".


  •  Right click on AppDelegate.m then select new file , select Objective C class, Click "Next" button, give th name as "GetaQuoteViewController" and click on "Xib for user interface", Goto Next, then Create. It'll creates GetaQuoteViewController.h, GetaQuoteViewController.m, GetaQuoteViewController.xib.
  • Go to AppDelegate.m and add #import "GetaQuoteViewController.h"
  •  Create a class object in Did Finish Launching method
            UITabBarController *tab = [[UITabBarController alloc]init];
     ServicesViewController *services = [[ServicesViewController 
     alloc]initWithNibName:@"ServicesViewController" bundle:nil];

     ContactViewController *contact = [[ContactViewController 
     alloc]initWithNibName:@"ContactViewController" bundle:nil];

     GetaQuoteViewController *get = [[GetaQuoteViewController 
     alloc]initWithNibName:@"GetaQuoteViewController" bundle:nil];
  • We should give the title on TabBar so write the code below.
            UITabBarController *tab = [[UITabBarController alloc]init];
     ServicesViewController *services = [[ServicesViewController 
     alloc]initWithNibName:@"ServicesViewController" bundle:nil];
     services.title = @"SERVICES";

     ContactViewController *contact = [[ContactViewController 
     alloc]initWithNibName:@"ContactViewController" bundle:nil];
     contact.title = @"ContactUs";

     GetaQuoteViewController *get = [[GetaQuoteViewController 
     alloc]initWithNibName:@"GetaQuoteViewController" bundle:nil];
     get.title = @"Get-a-Quote";
  • Add get object to NSArray
            UITabBarController *tab = [[UITabBarController alloc]init];
     ServicesViewController *services = [[ServicesViewController 
     alloc]initWithNibName:@"ServicesViewController" bundle:nil];
     services.title = @"SERVICES";
     ContactViewController *contact = [[ContactViewController 
     alloc]initWithNibName:@"ContactViewController" bundle:nil];
     contact.title = @"ContactUs";
     GetaQuoteViewController *get = [[GetaQuoteViewController 
     alloc]initWithNibName:@"GetaQuoteViewController" bundle:nil];
     get.title = @"Get-a-Quote";
     NSArray *arrNames= [NSArray arrayWithObjects:services, 
     contact,get, nil];
     [tab setViewControllers:arrNames];

  • Now run the Appllication. It'll shows services Tab, ContactUs Tab and Get-a-Quote Tab.



  • We should disply list of items on Services Tab. Here the list items are MobileDevelopment, SoftwareDevelopment, WebDevelopment, CloudComputing. Fist drag the TableView on ServicesViewController.xib.
Goto  ServicesViewController.xib. Now click on the utilities


Now drag the TableView controller.


  • Right click on TableView Controller , map the Delegate and DataSource methods to the File's Owner
  • goto ServicesViewController.h and adopt protocols UItableViewDelegate and UITableViewDataSource in < > (Tag) symbol.
     @interface ServicesViewController :
     UIViewController<UITableViewDelegate, UITableViewDataSource>
     {
  
     }

@end
  • Take an NSMutableArray for display the list of elements in ServiceViewController.h
     @interface ServicesViewController :
     UIViewController<UITableViewDelegate, UITableViewDataSource>
     {
     NSMutableArray *arrLists;
     }
  • Go to ServicesViewController.m and write list of elements in ViewDidLoad method.
     -(void)viewDidLoad
     {
     [super viewDidLoad];
     arrLists = [[NSMutableArray 
     alloc]initWithObjects:@"MobileDevelopment", 
     @"SoftwareDevelopment",@"WebDevelopment", @"CloudComputing", 
     nil];
     // Do any additional setup after loading the view from its nib.
     }
  • Goto .h file and select UITableViewDatasource then go to Jump to definition.


It'll shows Delegate DataSource Methods.



This method shows we can display number of rows

  -(NSInteger)tableView:(UITableView *)tableView 
  numberOfRowsInSection:(NSInteger)section
  {
  return  [arrLists count];
  }

This method shows for numbers of columns in table view.

   -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
   {
    return 1;
   }       

For displaying data on each  cell write this method

// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

    - (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView 
    dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
               initWithStyle:UITableViewCellStyleDefault 
               reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...
  
    cell.textLabel.text=[arrLists objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    }


Now run the application. It'll shows Services List



Here when i click on MobileDevelopment it will Navigate to ViewController.For that create UINavigationController in AppDelegate.m

     UINavigationController *nav = [[UINavigationController
    alloc]initWithRootViewController:services];
  • Replace the service object and give UInavigation's object.
          NSArray *arrNames = [NSArray arrayWithObjects: services, 
    contact, get,nil];
    [tab setViewControllers:arrNames];
              
    NSArray *arrNames = [NSArray arrayWithObjects: nav, contact, 
    get, nil];
    [tab setViewControllers:arrNames];

And Add this method in ServicesViewController.m

    - (void)tableView:(UITableView *)tableView
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
    MobileDevViewController *mobile=[[MobileDevViewController 
    alloc]initWithNibName:@"MobileDevViewController" bundle:nil];
    [self.navigation pushviewcontroller:mobile];
    }

  • Now run the Application. It'll shows Navigation on Services tab.

Now we should add extra class

"MobileDevelopment","SoftwareDevelopment","WebDevelopment","CloudComputing" with .h, .m, .Xib


Goto .Xib and drag the tableView and map theDelegate and Datasourcr methods to files Owner and write UITableViewDelegate, UITableViewDataSource in Tag symbol and take an NSMutableArray to display Mobile Developing list.

#import <UIKit/UIKit.h>

@interface MobileDevViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *array;
}

@end

goto .m and write in ViewDidLoad

- (void)viewDidLoad
{
 [super viewDidLoad];
 // Do any additional setup after loading the view from its nib.
   
array = [[NSMutableArray alloc] initWithObjects:@"Android App.Development",@"iPhone App.Development",@"iPad App.Development",@"BlackBerry App.Development",@"Windows App.Development",@"Symbian App.Development",@"Others", nil];
}

Implement tableview methods write code as below

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView 
    dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
               initWithStyle:UITableViewCellStyleDefault 
               reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
   MobileTypesViewController *types=[[MobileTypesViewController 
   alloc]initWithNibName:@"MobileTypesViewController" bundle:nil];
   [self.navigationController pushViewController:types 
   animated:nil];
   
}

If we click on mobile development it'll shows next page.


Add a class name and give name as WebDevViewController.
and add MobileTypesViewController for display types of mobiles like iPhone Android Blackberry etc.



Drag a table view controller in .Xib and map Delegate and DataSource to files Owner.
Extend Delegate DataSource methods in .h

#import <UIKit/UIKit.h>

@interface WebDevViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *arr;
}
@end

goTo .m and write ViewDidLoad method

- (void)viewDidLoad
{
 [super viewDidLoad];
 // Do any additional setup after loading the view from its nib.
 arr=[[NSMutableArray alloc]initWithObjects:@"Ruby on Rails
Development",@"PHP Web.Development",@"Web App.Development",@"Java Web Development",@"Offshore Web Development",@"CakePhp Web Development",@"AJAX Development",@"c/c++ Development",@"Flash Development",@"Others", nil];
}

Now Implement the TableView methods in .m

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView 
    dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier] autorelease];
       
    }
    cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

Goto ServicesController.h Class and import Class names

#import <UIKit/UIKit.h>
#import "MobileDevViewController.h"
#import "WebDevViewController.h"
#import "MobileTypesViewController.h"
@interface ServicesViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
  NSMutableArray *arrLists; 
}

@end

Goto ServicesViewController.m class and modify in DidSelectLowAtIndex method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![arrLists objectAtIndex:indexPath.row]==indexPath.row)
    {
     MobileDevViewController *mobile=[[MobileDevViewController 
     alloc]initWithNibName:@"MobileDevViewController" bundle:nil];
     [self.navigationController pushViewController:mobile 
     animated:YES];
     }
    
     else if(indexPath.row==2)
     {
      WebDevViewController *web=[[WebDevViewController 
      alloc]initWithNibName:@"WebDevViewController" bundle:nil];
      [self.navigationController pushViewController:web 
      animated:YES];
      }
}

Now run the Application. If we click on Web Development It'll shows as below




Now add another class give name as SoftwareDevViewController




now go to .Xib and drag the tableViewController and map Delegate and DataSource methods.
and go to .h write UITableViewDelegate and UITableViewDatasource in Tag Symbol and take an Array

#import <UIKit/UIKit.h>

@interface SoftwareDevViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *array3;
}

@end

Goto .m and write methods in ViewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view from its nib.
array3=[[NSMutableArray alloc]initWithObjects:@"Rinch Int.Application Development",@"Database Design & Development",@"Content Management Systems Development",@"Browser Development",@"E-Commerce Solutions",@"SAP Solutions",@"Oracle Solutions", nil];  
}

And implement tableview methods in .m file

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [array3 count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
  
    UITableViewCell *cell = [tableView 
    dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
               initWithStyle:UITableViewCellStyleDefault 
               reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text=[array3 objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

Goto servicesViewController.h and import Header section that SoftwareDevViewController.h

#import <UIKit/UIKit.h>
#import "MobileDevViewController.h"
#import "WebDevViewController.h"
#import "MobileTypesViewController.h"
#import "SoftwareDevViewController.h"
@interface ServicesViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
  NSMutableArray *arrLists;
}

@end

goTo ServicesViewController.m extend code like below

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (![arrLists objectAtIndex:indexPath.row]==indexPath.row)
     {
      MobileDevViewController *mobile=[[MobileDevViewController 
      alloc]initWithNibName:@"MobileDevViewController" bundle:nil];
      [self.navigationController pushViewController:mobile 
      animated:YES];
      }
      else if(indexPath.row==1)
      {
      SoftwareDevViewController *soft=[[SoftwareDevViewController 
      alloc]initWithNibName:@"SoftwareDevViewController" 
      bundle:nil];
      [self.navigationController pushViewController:soft 
      animated:YES];
      }

      else if(indexPath.row==2)
      {
      WebDevViewController *web=[[WebDevViewController 
      alloc]initWithNibName:@"WebDevViewController" bundle:nil];
      [self.navigationController pushViewController:web 
      animated:YES];
      }
   }

Now run the Application. If we click on SoftwareDevelopment it'll shows list of developments




Now add another class and give name as CloudComViewController.



Now drag the WebView Controller



Go to .h file and declare the WebView controller.

#import <UIKit/UIKit.h>

@interface CloudComViewController : UIViewController{
    IBOutlet UIWebView *web;
}

@end

Goto .Xib and map this.



We should display cloud computing information on WebView controller so Goto .m file and
write code in ViewDidLoad method.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    NSURL *url=[[NSURL 
    alloc]initWithString:@"http://www.maisasolutions.com/CloudComputing"];
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
    [web loadRequest:request];
}


Now go to ServicesViewController.h and Import header file.

#import <UIKit/UIKit.h>
#import "MobileDevViewController.h"
#import "WebDevViewController.h"
#import "MobileTypesViewController.h"
#import "SoftwareDevViewController.h"
#import "CloudComViewController.h"
@interface ServicesViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
  NSMutableArray *arrLists; 
}

@end


Now go to ServicesViewController.m and extend the code in DidSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![arrLists objectAtIndex:indexPath.row]==indexPath.row)
    {
     MobileDevViewController *mobile=[[MobileDevViewController 
     alloc]initWithNibName:@"MobileDevViewController" bundle:nil];
     [self.navigationController pushViewController:mobile 
     animated:YES];
     }
     else if(indexPath.row==1)
     {
     SoftwareDevViewController *soft=[[SoftwareDevViewController 
     alloc]initWithNibName:@"SoftwareDevViewController" bundle:nil];
     [self.navigationController pushViewController:soft 
     animated:YES];
     }

     else if(indexPath.row==2)
     {
     WebDevViewController *web=[[WebDevViewController 
     alloc]initWithNibName:@"WebDevViewController" bundle:nil];
     [self.navigationController pushViewController:web 
     animated:YES];
     }  
     else if(indexPath.row==3)
     {
     CloudComViewController *cloud=[[CloudComViewController 
     alloc]initWithNibName:@"CloudComController" bundle:nil];
     [self.navigationController pushViewController:cloud 
     animated:YES];
     }

}

Now run the Application. It'll shows CloudComputing Information.



We should display Mobile types.

So Goto MobileTypesViewController.h

Declare WebView and NsInteger

#import <UIKit/UIKit.h>

@interface MobileTypesViewController : UIViewController
{
    IBOutlet UIWebView *web;
    NSInteger selectedindex;
}
@property(nonatomic,assign)NSInteger selectedindex;
@end

Map the WebViewController in .Xib

Go .m and write the code in ViewDidLoad method

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    if(selectedindex==0)
    {
     NSURL *url=[[NSURL 
     alloc]initWithString:@"http://www.maisasolutions.com/AndroidApplicationDevelopment"];
     NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
     [web loadRequest:request];
     }
     else if(selectedindex==1)
     {
     NSURL *url=[[NSURL 
     alloc]initWithString:@"http://www.maisasolutions.com/iPhoneApplicationDevelopment"];
     NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
     [web loadRequest:request];
     } 
     else if(selectedindex==2)
     {
     NSURL *url=[[NSURL 
     alloc]initWithString:@"http://www.maisasolutions.com/iPadApplicationDevelopment"];
     NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
     [web loadRequest:request];
     }   
    else if(selectedindex==3)
     {
     NSURL *url=[[NSURL 
     alloc]initWithString:@"http://www.maisasolutions.com/BlackberryApplicationDevelopment"];
     NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
     [web loadRequest:request];
     } 
     else if(selectedindex==4)
     {
        NSURL *url=[[NSURL 
        alloc]initWithString:@"http://www.maisasolutions.com/WindowsApplicationDevelopment"];
        NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
        [web loadRequest:request];
      }
    else if(selectedindex==3)
    {
        NSURL *url=[[NSURL 
        alloc]initWithString:@"http://www.maisasolutions.com/SymbianApplicationDevelopment"];
        NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
        [web loadRequest:request];
    } 
    else if(selectedindex==4)
    {
     NSURL *url=[[NSURL 
     alloc]initWithString:@"http://www.maisasolutions.com/MobileDevelopment"];
     NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
     [web loadRequest:request];
    }
}

Now Run the Application

If we click on AndroidApp Development it'll shows Android Info.



If we click on iPhoneApp Development it'll shows iPhone Info.



If we click on iPadApp Development it'll shows iPad Info.



If we click on BlackberryApp Development it'll shows BlackberryApp Info.



If we click on WindowsApp Development it'll shows WindowsApp Info.



Add class WebDevTypesViewController and SoftwareDevTypesViewController.

WebDevelopment, SoftwareDevelopment are same procedure as above concept.