URLSession data task

Example code for request

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:apiUrl]];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (error != nil) {
        DLog(@"Failed to get data from url %@, the error is %@", apiUrl, error);
        return;
    }
    if (data != nil) {
        NSDictionary *returnDict = (NSDictionary *) [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
        DLog(@"The return dictionary is %@", returnDict);
    } else {
        DLog(@"The data is null");
    }
}];
[task resume];

Need call [task resume]; command.

你可能感兴趣的:(URLSession data task)