iOS-UIWebview设置超时、加载失败、重新加载等问题解决

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];
        });
    });
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值