CODEX-5 JSON FETCHING OBJECTIVE C

JSON FETCHING AND DISPLAYING IN OBJECTIVE-C

//  BlogPost.m

//  BlogApp

//

//  Created by Aashish on 13/04/16.

//  Copyright © 2016 JMataji. All rights reserved.

//

#import “BlogPost.h”

@implementation BlogPost

-(id)initWithTitle:(NSString *)title

{

    self = [super init];

    if(self)

    {

        self.title;

        self.author = nil;

        self.thumbnail = nil;

    }

    return self;

}

+(id)initWithBlogPost:(NSString *)title

{

    return [[self alloc]initWithTitle:title];

}

-(NSURL *)thumbnailURL

{

    return [NSURL URLWithString:self.thumbnail];

}

@end

//

//  TableViewController.m

//  BlogApp

//

//  Created by Aashish on 12/04/16.

//  Copyright © 2016 JMataji. All rights reserved.

//

#import “TableViewController.h”

#import “BlogPost.h”

@interface TableViewController ()

@end

@implementation TableViewController

@synthesize blogPosts;

– (void)viewDidLoad {

    [super viewDidLoad];

    

//    BlogPost *blogPost = [[BlogPost alloc] initWithTitle:@”Some Title”];

//

//    blogPost.author = @”some author”;

//    

//    BlogPost *blogPost1 = [BlogPost initWithBlogPost:@”Another title”];

//    blogPost1.author = @”Aashish Bapodra”;

    NSURL *Url = [NSURL URLWithString:@”http://blog.teamtreehouse.com/api/get_recent_summary/”];

    

    NSData *JSONData = [NSData dataWithContentsOfURL:Url];

    

    

    

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization  JSONObjectWithData:JSONData options:0 error:&error];

    

//    NSDictionary *blogPost1 = [NSDictionary dictionaryWithObjectsAndKeys:@”Ranok”,@”title”,@”john”,@”author” ,nil];

//     NSDictionary *blogPost2 = [NSDictionary dictionaryWithObjectsAndKeys:@”Rahul”,@”title”,@”John”,@”author” ,nil];

//     NSDictionary *blogPost3 = [NSDictionary dictionaryWithObjectsAndKeys:@”Rony”,@”title”,@”John”,@”author” ,nil];

    

    

    self.blogPosts = [[NSMutableArray alloc]init];

    

    

    

    NSArray *blogPostArray = [dataDictionary objectForKey:@”posts”];

    

    

    

    for (NSDictionary *dpDic in blogPostArray) {

        

        BlogPost *blogData = [BlogPost initWithBlogPost:[dpDic objectForKey: @”title”]];

        blogData.title = [dpDic objectForKey:@”title”];

        blogData.author = [dpDic objectForKey:@”author”];

        blogData.thumbnail = [dpDic objectForKey:@”thumbnail”];

        blogData.date = [dpDic objectForKey:@”date”];

        

        [self.blogPosts addObject:blogData];

        

    }

    

    

   }

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark – Table view data source

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [blogPosts count] ;

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”Cell” forIndexPath:indexPath];

    

    

    BlogPost *blogPost1 = [self.blogPosts objectAtIndex:indexPath.row];

    if([blogPost1.thumbnail isKindOfClass:[NSString class]])

    {

    NSData *im = [NSData dataWithContentsOfURL:blogPost1.thumbnailURL];

        

        UIImage *images = [UIImage imageWithData:im];

          cell.imageView.image = images;

    }

    else

    {

        cell.imageView.image = [UIImage imageNamed:@”hs.jpg”];

    }

    

   

  

    cell.textLabel.text = blogPost1.title;

    cell.detailTextLabel.text =  [NSString stringWithFormat:@”Author And Date %@ %@”,blogPost1.author,blogPost1.date];

    

    

    return cell;

}

@end

Leave a Reply

Your email address will not be published. Required fields are marked *