iOS-UIWebView 请求百度数据加载到页面 同步/异步/NSURLSession

@interface ViewController ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>

{
    NSMutableData *netData; // 拼装网络数据
}

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 在iOS开发里 Connetcion和session 是在foundation框架里面数据同一层级,不同的地方就是7.0以后Session 更多的是在后台请求方面做了很大的优化,还有就是本地存储的优化
//    NSURLConnection
//    NSURLSession


//  -----------异步加载网络数据---------
    // 初始化一个网络地址对象
    NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
    
    // 初始化一次网络请求
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    
    // 拼装网络请求的数据
    netData=[[NSMutableData alloc]init];
    
    //初始化网络请求链接
//    NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
////     开始请求
//    [connection start];

    
// ---------------同步加载数据--------------
    // 同步加载网络数据
//    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com"]];
//    NSString *s=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
//    NSLog(@"%@",s);
    
//  同步和异步的区别
//  同步去做网络数据请求的时候,必须得等数据请求下来才能做其他的操作,在数据没有下载下来之前都会卡住界面,不会执行下面的代码
//  异步异步数据请求的时候,不会卡住主界面,一边做数据请求一边还可以做其他代码操作
    
//    UIImage *image=[UIImage imageWithData:data];
//    UIImageView *image_V=[[UIImageView alloc]initWithFrame:self.view.bounds];
//    [image_V setImage:image];
//    [self.view addSubview:image_V];
 
    
//    ------------NSURLSession---------
    // NSURLSession 是iOS 7之后苹果比叫推崇的网络请求方式,是苹果自己封装的网络请求,以block方式返回数据
   
    NSURL *sessionUrl=[NSURL URLWithString:@"http://1000phone.net:8088/app/iAppFree/api/limited.php?page=1&number=20"];
    NSURLRequest *sessionRequest=[NSURLRequest requestWithURL:sessionUrl];
    // 创建一个Session网络请求 
   NSURLSession *session=[NSURLSession sharedSession];
    // 创建一次网络数据请求的任务
    NSURLSessionDataTask *task=[session dataTaskWithRequest:sessionRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
//        NSString *s=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",s);
        // 把网络请求的JSON数据转换成NSDictionary
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        NSString *count=dic[@"count"];
        NSArray *applist=dic[@"applications"];
        NSDictionary *appInfo=applist.firstObject;
        NSString *name=appInfo[@"name"];
        
        
        NSLog(@"%@",count);
//        NSLog(@"%@",applist);
        NSLog(@"%@",appInfo);
        
        
    }];
    
    //提交任务
    [task resume];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ---网络请求的代理

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"网络请求错误");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"接受到数据");
    NSLog(@"下载的数据%ld",data.length);
    [netData appendData:data];// 把每一次请求的数据,放到我的内存里面
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"第一次建立连接完成");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"网络请求完成");
    
    // 把请求完成的二进制数据转换成字符串数据
    NSString *baiduHtml=[[NSString alloc]initWithData:netData encoding:NSUTF8StringEncoding];
    // 打印数据
    NSLog(@"百度首页的数据:%@",baiduHtml);
//    
    UIWebView *web=[[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:web];
    // 字符串的方法加载web页面
    [web loadHTMLString:baiduHtml baseURL:nil];
//    
}

你可能感兴趣的:(iOS-UIWebView 请求百度数据加载到页面 同步/异步/NSURLSession)