在NSURLRequest下面调用UIWebView后,测试会奔溃
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 1.创建url
// 请求一个网页
NSString *urlString = @"http://www.xinsandai.com/api/get_JsonNewsInfo.php?pid=2084";
// 一些特殊字符编码
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
// 2.创建请求 并:设置缓存策略为每次都从网络加载 超时时间30秒
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
// 3.采用苹果提供的共享session
NSURLSession *sharedSession = [NSURLSession sharedSession];
// 4.由系统直接返回一个dataTask任务
NSURLSessionDataTask *dataTask = [sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 网络请求完成之后就会执行,NSURLSession自动实现多线程
NSDictionary*dicRoot = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingMutableContainers error:nil];
if (data && (error == nil)) {
// 网络访问成功
//NSLog(@"data=%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NewsInfoModel*model = [[NewsInfoModel alloc] init];
model.mTitle = [dicRoot objectForKey:@"title"];
model.mBody = [dicRoot objectForKey:@"body"];
model.mID = [dicRoot objectForKey:@"aid"];
//NSLog(@"%@",model.mBody);
UILabel*title = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.bounds.size.width-20, 40)];
title.text = model.mTitle;
title.numberOfLines = 2;
title.font = [UIFont systemFontOfSize:14];
[self.view addSubview:title];
CGRect bouds = [[UIScreen mainScreen] applicationFrame];
_webView = [[UIWebView alloc] initWithFrame:bouds];
_webView.scalesPageToFit = YES;
_webView.delegate = self;
[_webView loadHTMLString:@"<p>Hello</p>" baseURL:nil];
[self.view addSubview:_webView];
} else {
// 网络访问失败
NSLog(@"error=%@",error);
}
}];
// 5.每一个任务默认都是挂起的,需要调用 resume 方法
[dataTask resume];
}
测试程序会奔溃,单独调用UIWebView没问题
希望大大们指教