Skip to content

Response Parsing, Error Handling 2

Alfie Hanssen edited this page Nov 22, 2013 · 4 revisions

You should have something like this:

NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    
    if (error) {
        NSLog(@"error! %@", error.localizedDescription);
    } else {
        
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
                                                                   options:NSJSONReadingAllowFragments
                                                                     error:&error];
        if (error){
            NSLog(@"error! %@", error.localizedDescription);
        } else {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            if (httpResponse.statusCode == 200) {
                NSLog(@"success! %@", dictionary);
            } else {
                NSLog(@"error! %@", dictionary);
            }
        }
    }
    
}];
Clone this wiki locally