1

我有一个带有标准视图控制器的应用程序,上面有多个按钮。每个按钮都链接到具有唯一 UIWebView 的单独视图控制器。每个 UIWebView 都实现了 didFailLoadWithError 并且似乎工作正常:当我关闭 wifi 并尝试从主视图控制器页面加载 UIWebView 时,我正确地从 didFailLoadWithError 得到错误消息。当我打开 wifi 并加载 UIWebView 时,它工作正常 - 没有错误。但是,当我单击该 UIWebView 页面中的链接时,我再次收到 didFailLoadWithError 错误。更有趣的是,我清除了错误消息,新页面仍然从我刚刚单击的链接加载,所以我知道连接良好。这是我的实现...

@synthesize webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Alert"    message:@"No Internet Connection - Please Check Your Network Settings and Try Again" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alert show];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:     @"http://www.site.com/index.html"]]];
    [webView addSubview:activity];
    timer=[NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
        target:self selector:@selector(loading) userInfo:nil repeats:YES];
           }

- (void)loading {
    if (!webView.loading)
        [activity stopAnimating];
        else
            [activity startAnimating];
}
4

1 回答 1

2

我刚遇到这个问题。发生了什么,当您单击 Web 视图中的链接时,当页面仍在加载时,您将收到 error -999。这转化为NSURLErrorCancelled.

您可以在以下链接中找到更多信息。转到该URL Loading System Error Codes部分。https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html

在我的代码中,我告诉一个警报视图弹出,说互联网连接在-webView:didFailLoadWithError:被调用时丢失。我将该代码包装在错误对象的条件周围。这是一个例子。

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    if ([error code] == NSURLErrorNotConnectedToInternet || [error code] == NSURLErrorNetworkConnectionLost) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Check internet connection." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    }
}
于 2014-01-22T06:15:53.490 回答