美文网首页
URLSession data task

URLSession data task

作者: liwp_Stephen | 来源:发表于2016-11-22 20:10 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/pnkmpttx.html