加载web请求
– (void)viewDidLoad {
NSURLRequest* request = [NSURLRequestrequestWithURL: [NSURLURLWithString:self.urlStr]cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:8];
[self.webViewloadRequest:request];
}
加载失败
-(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error{
//有网络就是用数据请求,没有网络就是用缓存
//没有缓存就提示网络连接失败
NSURLCache*cache = [NSURLCachesharedURLCache];
NSCachedURLResponse*response = [cachecachedResponseForRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:self.urlStr]]];
if(response ==nil) {
//显示错误界面 点击重新加载
}else{
NSURLRequest* request = [NSURLRequestrequestWithURL: [NSURLURLWithString:self.urlStr]cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval:8];
[self.webViewloadRequest:request];
}
}
重新加载方法
-(void)reloadWebview{
NSURLRequest* request = [NSURLRequestrequestWithURL:[NSURLURLWithString:self.urlStr]cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval:8];
[self.webViewstopLoading];
[self.webViewstopLoading];
[self.webViewloadRequest:request];
}
重新加载使用的还是使用缓存,不去请求
errorError Domain=NSURLErrorDomain Code=-999 “(null)”
void SendDelegateMessage(NSInvocation *): delegate (webView:didFailProvisionalLoadWithError:forFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
-999错误说是请求取消导致的错误
kCFRunLoopDefaultMode 据说是webView抢占主线程导致
最后排查了很久才发现是请求的时候使用了(NSURLRequestReturnCacheDataDontLoad)缓存请求 web 请求,因为没有缓存导致一直出错把缓存策略改成NSURLRequestUseProtocolCachePolicy就可以解决问题