iOS-UIWebview设置超时、加载失败、重新加载等问题解决
2017-07-25 14:25
1271 查看
iOS开发 UIWebView加载失败 我想重新加载怎么做,使用reload不顶用
解决办法:重新使用loadRequest:
设置超时
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:2]; UIWebView* webView = [[UIWebView alloc]initWithFrame:self.view.bounds]; webView.delegate=self; [webView loadRequest:request]; [self.view addSubview:webView]; }
超时额外提示或者其他自定义操作
- (void)viewDidLoad { [super viewDidLoad]; UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; [self.view addSubview:myWebview]; NSURL *url = [NSURL URLWithString:@"www.baidu.com"]; NSURLRequest *request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0.5]; [webview loadRequest:request]; if (theConnection) { [theConnection cancel]; NSLog(@"safe release connection"); } theConnection= [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES]; } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if (theConnection) { NSLog(@"safe release connection"); } if ([response isKindOfClass:[NSHTTPURLResponse class]]){ NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; if ((([httpResponse statusCode]/100) == 2)){ NSLog(@"connection ok"); } else{ NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:nil]; if ([error code] == 404){ NSLog(@"404"); } } } } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ if (theConnection) { NSLog(@"safe release connection"); } if (error.code == 22) //The operation couldn’t be completed. Invalid argument NSLog(@"22"); else if (error.code == -1001) //The request timed out. webview code -999的时候会收到-1001,这里可以做一些超时时候所需要做的事情,一些提示什么的 NSLog(@"-1001"); else if (error.code == -1005) //The network connection was lost. NSLog(@"-1005"); else if (error.code == -1009){ //The Internet connection appears to be offline NSLog(@"-1009"); } }
相关网络加载失败提示封装
HCWProgressHUD *hud = [HCWProgressHUD showHUDAddedTo:self.view animated:YES tapContentBlock:^(HCWProgressHUDMode mode) { } clickButtonBlock:^(HCWProgressHUDMode mode) { }]; hud.buttonCorlor = [UIColor blueColor]; // 没有网络 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ hud.mode = HCWProgressHUDModeNoInternet; // 没有数据 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ hud.mode = HCWProgressHUDModeNoData; // 隐藏 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [HCWProgressHUD hideHUDForView:self.view animated:YES]; }); }); });
相关文章推荐
- 解决iOS中使用UIWebView加载嵌入脚本中带有alert时卡住界面问题。
- 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreat的问题解决
- 隐藏TabBar后,UIWebView加载过程中和加载失败时,底部出现黑边的问题解决
- 如何正常设置VS 2005 字体大小与颜色(附助解决包加载失败的问题)
- angular-pdf 在IOS平台加载多页PDF 失败问题解决
- iOS重新设置首界面不能启动问题的解决方法
- 【IOS 开发】UIWebView 加载本地文件,多层的网页目录结构失败分析和解决,以及demo
- 【iOS进阶】UIWebview加载搜狐视频,自动跳回客户端 问题解决
- Requirejs加载超时问题的一个解决方法:设置waitSeconds=0
- 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreat的问题解决
- 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreate的问题解决
- 解决Weblogic 配置连接池加载数据库驱动失败问题
- Ubuntu设置静态IP,解决重启后需要重新设置的问题
- C#Socket开发“网络连接超时设置Timeout”,解决网络连接速度过慢的问题
- 故障解决:VS2005的水晶报表在WEB应用程序中多次使用后,就会出现加载报表失败.重启WEB服务器又正常了.过一段时间又出同样问题
- SVN提交时出错,重新提交提示locked,Cleanup清理失败问题的解决方法
- 解决串口通讯中部分串口ReadFile使用SetCommTimeouts设置超时无效的问题
- Windows Server 2003 下Apache加载PHP失败的问题解决
- 解决Com类组建加载失败的问题!
- 关于Firefox重新加载flex的问题解决