Wednesday 20 November 2013

sample JSON Parser using global declaration in iOS

                                                                  JSON Parser using iOS

Hi ,

This Mohan iOS application developer ,I learn objective c and Xcode Development tool kit.Now I'm going talk about JSON Parser using global declaration in iOS your apps.Let's follow my steps.


Stap 1:Install your Latest Xcode 4.6.3 or later
Step2:Open xcode go File->select the New option ->Projects
Step3:Choose template for your project see the screen shot.I have selected single view application.and click the Next option


Step4:Let's fill the following fields in below screen shot.Project Name and your Organisation name and company identifier with select device option like you should use only in iPhone means select iPhone other wise you need iPhone and iPad select universal option.click the Next option

step5:set your Project folder path and click the create option.Project has been successfully created .see the screen shot.
Step6:Run your project select simulator (Run cmd+R)
Step7:Add the NSobject attached file in your Project DownloadManager.h and downloadManager.m go to jsonparser xcode andchoose build phases->compile source and click the Plus symbol add the your downloadManger.m file
Step8:downloader.h file  I written below codes here

#import <Foundation/Foundation.h>

@interface downloadManager : NSObject<NSURLConnectionDelegate>
{
    id callingclassObject;
    NSMutableDictionary *dataDictionary;
    NSMutableData *responseData;
    NSString *keyString;
    BOOL howevergetcontroll_back;
}
@property(unsafe_unretained)BOOL howevergetcontroll_back;
@property(nonatomic,strong)NSString *keyString;
@property(nonatomic,strong)NSMutableData *responseData;
@property(nonatomic,strong)NSMutableDictionary *dataDictionary;
-(void)startingloadingFromURL:(NSString *)urlstring delegate:(id)delegate withkey:(NSString *)key;
-(void)didFinishLoading:(NSDictionary *)responcedic key:(NSString *)key;
@end
Step9:downloader.M file  I written below codes here
#import "downloadManager.h"
#import "AppDelegate.h"
@implementation downloadManager
@synthesize responseData,dataDictionary,keyString,howevergetcontroll_back = _howevergetcontroll_back;
-(void)startingloadingFromURL:(NSString *)urlstring delegate:(id)delegate withkey:(NSString *)key
{
    
        keyString = key;
        callingclassObject = delegate;
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:[NSURL URLWithString:urlstring]];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];
}
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
    self.dataDictionary=[[NSMutableDictionary alloc]init];
    self.responseData=[[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    if (data) {
        [self.responseData appendData:data];        
    }
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"Sorry!" message:@"Some error has occurred please try agian" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertview show];
    
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.dataDictionary = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:nil];
       
    if ([callingclassObject respondsToSelector:@selector(didFinishLoading:key:)])
    {
        [callingclassObject didFinishLoading:self.dataDictionary key:self.keyString];
    }
    
    if ([[[self.dataDictionary valueForKey:@"response"] valueForKey:@"status"] intValue] == 0 && [[[self.dataDictionary valueForKey:@"response"] valueForKey:@"message"] length])
    {
        UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"" message:[[self.dataDictionary valueForKey:@"response"] valueForKey:@"message"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertview show];
    }
    
    if ([self.dataDictionary isEqual:[NSNull null]])
    {
        UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"Sorry!" message:@"Some error has occurred please try agian" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertview show];
    }
    
    [AppDelegate stopIndicator];
}
Stop10:Next go to Viewcontoller.m file import  the download manger .h file  and write down the some codes in viewDidLoad parer your API url here and get the response in didFinishLoading:

        #import "ViewController.h"
#import "downloadManager.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    // allocate download Manger 
    downloadManager *object=[[downloadManager alloc]init];
    // add the object you api url 
    [object startingloadingFromURL:@"your url in string formate" delegate:self withkey:@"api key name"];
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
// get he Json response below code....
-(void)didFinishLoading:(NSDictionary *)responcedic key:(NSString *)key
{
    NSLog(@"Json respone %@ and api key name %@",responcedic,key);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


Step11: Let do you other functionality ..What you show in your project ..Etc..