json Get

app link: https://drive.google.com/open?id=0BwU5Y4Xqfa42SU1HUWpyNDQ2ckk
                https://drive.google.com/open?id=0BwU5Y4Xqfa42NVRpaGNiUHl1N0k



view.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UISegmentedControl *seg;
- (IBAction)userSeg:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property NSMutableArray *ary,*imageAry,*disAry;
@property NSMutableDictionary *dic;
@property NSURLSessionDataTask *data;
@property NSURLRequest *urlFreeReq,*urlPaidReq,*urlPopularReq;
@property NSURLSession *urlSession;

@end

View.m

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _ary=[[NSMutableArray alloc]init];
    _imageAry = [[NSMutableArray alloc]init];
    _disAry=[[NSMutableArray alloc]init];
    _dic=[[NSMutableDictionary alloc]init];
    _urlFreeReq=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://itunes.apple.com/in/rss/topfreeapplications/limit=100/json"]];
    _urlPaidReq=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://itunes.apple.com/in/rss/toppaidapplications/limit=100/json"]];
    _urlPopularReq=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://itunes.apple.com/in/rss/topgrossingapplications/limit=100/json"]];
    _urlSession=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)userSeg:(id)sender {
    if (_seg.selectedSegmentIndex==0) {
        [_ary removeAllObjects];
        [_imageAry removeAllObjects];
        _data=[_urlSession dataTaskWithRequest:_urlFreeReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            _dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            for (int i=0; i<99; i++) {
                [_ary addObject:[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:name"]objectForKey:@"label"]];
                 NSLog(@"%@",self.ary);
                NSString *imageUrl=[[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
                NSData *data1=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
                [self.imageAry addObject:[UIImage imageWithData:data1]];
                [_disAry addObject:[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"summary"]objectForKey:@"label"]];
               
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView reloadData];
            });
           
        }];
    }else if(self.seg.selectedSegmentIndex==1){
        [_ary removeAllObjects];
        [_imageAry removeAllObjects];
        _data=[_urlSession dataTaskWithRequest:self.urlPaidReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
           
            _dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            for (int i=0; i<99; i++) {
                [_ary addObject:[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:name"]objectForKey:@"label"]];
                NSString *imgStr=[[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
                NSData *data1=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgStr]];
                [_imageAry addObject:[UIImage imageWithData:data1]];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView reloadData];
            }); 
        }];
    }
    else{
        [_ary removeAllObjects];
        [_imageAry removeAllObjects];
        _data=[_urlSession dataTaskWithRequest:_urlPopularReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            _dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            for (int i=0; i<99; i++) {
                [_ary addObject:[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:name"]objectForKey:@"label"]];
                NSString *imgStr=[[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
                NSData *data1=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgStr]];
                [_imageAry addObject:[UIImage imageWithData:data1]];
                [_disAry addObject:[[[[[_dic objectForKey:@"feed"]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"summary"]objectForKey:@"label"]];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView reloadData];
            });
        }];
    }
    
    [self.data resume];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    return [self.ary count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier=@"id";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text=[NSString stringWithFormat:@"%lu %@",(indexPath.row +1),[self.ary objectAtIndex:indexPath.row]];
    cell.imageView.image=[_imageAry objectAtIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailViewController *detail=[self.storyboard instantiateViewControllerWithIdentifier:@"DVC"];
    [self presentViewController:detail animated:YES completion:^{
        detail.label.text=[_ary objectAtIndex:indexPath.row];
        detail.imageView.image=[_imageAry objectAtIndex:indexPath.row];
        detail.textView.text=[_disAry objectAtIndex:indexPath.row];
    }];
}
@end

detailView.h

#import <UIKit/UIKit.h>

@interface detailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)backBtn:(id)sender;
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

detailView.m

#import "detailViewController.h"

@interface detailViewController ()

@end

@implementation detailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)backBtn:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
Previous
Next Post »

1 comments:

Write comments
adithyaa
AUTHOR
29 January 2017 at 22:28 delete

its throwing exception
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:

Reply
avatar