How to send a Get request in iOS?

后端 未结 4 739
野的像风
野的像风 2020-12-04 16:37

I am making a library to get response from a particular URL with specified data and method type. For this, I am making a request with url. But when I set its method type, it

4条回答
  •  忘掉有多难
    2020-12-04 17:31

    Simply call and use:

    (void)jsonFetch{
    
        NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"];
    
        NSURLSession *session = [NSURLSession sharedSession];
    
        NSURLSessionDataTask *data = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    
            NSError *erro = nil;
    
            if (data!=nil) {
    
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&erro ];
    
                if (json.count > 0) {
    
                    for(int i = 0; i<10 ; i++){
    
                        [arr addObject:[[[json[@"feed"][@"entry"] objectAtIndex:i]valueForKeyPath:@"im:image"] objectAtIndex:0][@"label"]];
    
                    }
    
                }
            }
            dispatch_sync(dispatch_get_main_queue(),^{
    
                [table reloadData];
            });
        }];
    
        [data resume];
    }
    

提交回复
热议问题