Wednesday, February 13, 2013

json with IOS (Basic) - Tutorial



What we are going to do?




                      

json Array

[{"lt_id":"1","lt_name":"Normal","lot_id":"8","lot_name":"Jayoda","lot_code":"JA15011","lot_price":"17.00","lot_book_size":"100","lot_status":"Active"},{"lt_id":"1","lt_name":"Normal","lot_id":"9","lot_name":"Monday Jayoda","lot_code":"LJM","lot_price":"18.00","lot_book_size":"100","lot_status":"Active"},{"lt_id":"1","lt_name":"Normal","lot_id":"10","lot_name":"Monday Super Ball","lot_code":"BSB","lot_price":"17.00","lot_book_size":"100","lot_status":"Active"},{"lt_id":"1","lt_name":"Normal","lot_id":"11","lot_name":"Tuesday - Development Fortune","lot_code":"ODT","lot_price":"18.00","lot_book_size":"100","lot_status":"Active"},{"lt_id":"1","lt_name":"Normal","lot_id":"12","lot_name":"nkinon","lot_code":"","lot_price":"0.00","lot_book_size":"0","lot_status":""},{"lt_id":"1","lt_name":"Normal","lot_id":"14","lot_name":"nhgjgk88...","lot_code":"vhgjgb999...","lot_price":"99999.00","lot_book_size":"78","lot_status":"bvnvnv88..."},{"lt_id":"2","lt_name":"Instant","lot_id":"13","lot_name":"bscbxabskjakj2324","lot_code":"xsckbdihsbkk878e2787","lot_price":"324424.00","lot_book_size":"243254","lot_status":"adhckabgjcbsdbk"}]


Explain about Json array


  •  Table:-

  • Json Array  of that table:-


          [{  it_id, It_name, lot_id,  lot_name, lot_code, lot_price,   lot_book_size,  lot_status }]



Method :-


ViewController.xib








AppDelegate.m
  add this code to application didFinishLaunchingWithOptions method in AppDelegate..

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}

ViewController.h


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UITableView *mainTableView;
   
    IBOutlet UITextView *textShow;
   
    NSArray *news;
    NSMutableData *data;
}

@end


 ViewController.m


#import "ViewController.h"
#import "DetailViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Adrian New JasonTest";
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

     // In here URL will be your one...
    NSURL *url = [NSURL URLWithString:@"http://-----Your Link-------------.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
      NSLog(@"loading");
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
    NSLog(@" Response");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
     NSLog(@" Recived");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
   
    news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
   
    [mainTableView reloadData];
     NSLog(@"finish loading");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}





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

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


//-------------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
   
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    }
   
   
    //--show details -------------------------
    //Json Array
    //    [{  it_id, It_name, lot_id,  lot_name, lot_code, lot_price,   lot_book_size,  lot_status }]

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"lot_name"]; 
// get lot_name according to you json array @"lot_name"
    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"lot_id"];
 // get lot_id  according to you json array  @"lot_id"
    return cell;

 //-----------------------------------------------------------------------------------
    
}

  //go to DetailViewController------------------------------------------------------------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
   
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.title = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
    detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailViewController animated:YES];
    
}




- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end


Now we are going to create second view ….
We call that DetailViewController






DetailViewController.xib








DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController {
    NSDictionary *newsArticle;
   
    IBOutlet UILabel *titleLabel;
    IBOutlet UILabel *timeLabel;
   
    IBOutlet UITextView *descTextView;
}

@property (nonatomiccopyNSDictionary *newsArticle;

@end

DetailViewController.m


#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize newsArticle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   //Json Array
    //    [{  it_id, It_name, lot_id,  lot_name, lot_code, lot_price,   lot_book_size,  lot_status }]

    titleLabel.text = [newsArticle objectForKey:@"lot_name"];//Show detail of lot name
    timeLabel.text = [newsArticle objectForKey:@"lot_code"];//Show lot code
   
    descTextView.text = [newsArticle objectForKey:@"lot_price"];//Show lot price
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end



Build & Run the Project