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];
        });
    });
});

你可能感兴趣的:(UIWebView)