远程数据解析最新方法NSURLSession

.m文件

//获取文件访问路径

    NSString *path=@"http://1.studyios.sinaapp.com/getAllClass.php";

    //封装URL

    NSURL *url=[NSURL URLWithString:path];

    //创建请求命令

    NSURLRequest *request=[NSURLRequest requestWithURL:url];

    //创建会话对象 通过单例方法实现

    NSURLSession *session=[NSURLSession sharedSession];

    //执行会话的任务,通过request请求 获取data对象

    NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        //json解析

        NSArray *arrjson=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

        NSLog(@"%@",arrjson);

    }];

    //代码块回调

    [task resume];

原文地址:https://www.cnblogs.com/anjiubo/p/5317392.html