JSon Parsing



In Your class

//Call
[self parseByHUD];


-(void)parseByHUD
{
    HUD = [[MBProgressHUD alloc] initWithView:appDelegate.window];
    HUD.tag=2;
    HUD.graceTime = .1;
    HUD.labelFont  = [UIFont fontWithName:@"Arial" size:16];
    //HUD.labelText = @"Authenticating";
    HUD.delegate = self;
    [appDelegate.window addSubview:HUD];
    [HUD showWhileExecuting:@selector(JSONMethod) onTarget:self withObject:nil animated:YES];
}

-(void)hudWasHidden:(MBProgressHUD *)hud
{
    [mainTableView reloadData];
}

-(void)JSONMethod
{
    //Your URL
    NSString *urlString = [NSString stringWithFormat:@"%@city.php",SiteAPIURL];

    id result = [webServiceParsing  getDataFromURLString:urlString];
}




In  webServiceParsing.h

//Json Parsing
+(id) getDataFromURLString :(NSString*)urlString
{
    NSLog(@"****IN****");
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    NSLog(@"URL > %@",url);
    NSError* error = nil;
    NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
    } else {
        NSLog(@"Data has loaded successfully.");
    }

    if (data == nil)
    {
        [self performSelectorOnMainThread:@selector(serverError) withObject:nil waitUntilDone:YES];
        return nil;
    }
    else
    {
        NSError *err;
        id result=[NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &err];

        return result;
    }
    NSLog(@"****Out****");
}
-(void)serverError
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error !!!" message:@"Server Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

Comments

Popular posts from this blog

How to Make REST api call in Objective-C.

Launch iPhone simulator without Xcode.

Hacker Rank problem solution