Skip to content

Instantly share code, notes, and snippets.

@xor-gate
Forked from r3econ/gist:9923319
Created June 11, 2016 14:38
Show Gist options
  • Save xor-gate/5975a645bd8772049866850a92259aac to your computer and use it in GitHub Desktop.
Save xor-gate/5975a645bd8772049866850a92259aac to your computer and use it in GitHub Desktop.
Downloading JSON file using NSURLSession.
// Create a sample URL.
NSURL *url = [NSURL URLWithString:@"http://www.bbc.co.uk/tv/programmes/genres/drama/scifiandfantasy/schedules/upcoming.json"];
// Create a download task.
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error)
{
if (!error)
{
NSError *JSONError = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&JSONError];
if (JSONError)
{
NSLog(@"Serialization error: %@", JSONError.localizedDescription);
}
else
{
NSLog(@"Response: %@", dictionary);
}
}
else
{
NSLog(@"Error: %@", error.localizedDescription);
}
}];
// Start the task.
[task resume];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment