-3

I want the display the output of a URl using NSLog.

ViewController.h

@interface ViewController : UIViewController{
NSURLSession *session;
NSString *londonWeatherUrl;
NSURLRequest *request;
}

ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

londonWeatherUrl = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0";

request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:londonWeatherUrl]];


[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
        completionHandler:^(NSData *data,
                            NSURLResponse *response,
                            NSError *error) {}] resume];

session = [NSURLSession sharedSession];
NSLog(@"%@",);  // what goes here ?
}

Can someone tell me what do i need to have inside NSLog so that the output of that URL is displayed in the log. Sorry, i am a beginner in objective C.

Tejas K
  • 682
  • 11
  • 28
  • "output of a Url" - do you mean "Response"? – Sandr Jan 14 '16 at 09:27
  • @Sandr - yes, the response – Tejas K Jan 14 '16 at 09:28
  • The `NSLog` goes to that completion handler (between `{` and `}`). Your `data` is a parameter of the completion handler. You can display that data in hexadecimal format or you will have to parse it first (JSON, UIImage etc). – Sulthan Jan 14 '16 at 09:34

2 Answers2

1

You can handle response calling:

 - (void)viewDidLoad {
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

londonWeatherUrl = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0";

request = [NSURLRequest requestWithURL:
                     [NSURL URLWithString:londonWeatherUrl]];


[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
    completionHandler:^(NSData *data,
                          NSURLResponse *response,
                          NSError *error) {
        // handle response
        NSLog(@"%@", response);

 }] resume];
}
Sandr
  • 189
  • 9
1

Here is your response code.

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

londonWeatherUrl = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0";

request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:londonWeatherUrl]];


    [[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {

        NSError* error;
        NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; //Provide your NSData here to convert to dictionary

        //NSArray *responses = [json objectForKey:@"YOUR KEY"]; //Provide your key if contain array or else directly use as dictionary

        NSLog(@"Data dictionary: %@", jsonDictionary);
        NSLog(@"NSURLResponse: %@", response);

}] resume];

session = [NSURLSession sharedSession];    

} 
nikhil84
  • 3,235
  • 4
  • 22
  • 43