iPhone - UIWebView 在没有数据连接的情况下不调用 didFailLoadWithError

发布于 2024-12-03 03:01:38 字数 1476 浏览 0 评论 0原文

我已经搜索过,但没有找到解决方案,所以希望有人能提供帮助。

我有一个 UIWebView,当用户按下按钮时会调用它。一切都运行良好,除非您考虑手机没有数据连接时的行为。

当手机处于飞行模式或没有数据连接时第一次尝试打开 UIWebView 时:

  • UIWebView 开始加载
  • 加载失败

一旦 UIWebView 被分配并且第一次加载失败,点击链接到 IBAction 的刷新按钮(调用 [webView 重新加载])会导致:

  • 无论手机在初始加载失败后是否有数据连接,都会调用刷新 IBAction,但我的 NSLogs 都没有告诉我调用 webViewDidStartLoad、webViewDidFinishLoad 或 webView...didFailLoadWithError。

如果我让应用程序保持运行状态并打开与设备的数据连接(例如关闭飞行模式),然后返回我的应用程序并点击刷新按钮,则会调用 [webView reload],但 webViewDidStartLoad (因此 webViewDidFinishLoad)没有被调用。

这是相关代码:

调出 webView:

-(IBAction)loadWebView {

    webView = [[UIWebView alloc] init];
    webView.scalesPageToFit = YES;
    [webView setDelegate:self];

    NSString *urlAddress = @"http://google.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [webView loadRequest:requestObj];

}

重新加载:

  -(IBAction)reloadWebView{
    [webView reload];
    NSLog(@"RELOAD");
}

webViewDidStart,DidFinish,didFailLoadWithError:

 - (void)webViewDidStartLoad:(UIWebView *)webView
{  
    NSLog(@"START LOAD");   
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
     NSLog(@"FINISH LOAD");    
}

- (void)webView:(UIWebView *) webView didFailLoadWithError:(NSError *)error 
{ 
       NSLog(@"DID FAIL");
}

想法?

I've searched and haven't found a solution to this, so hopefully someone can help.

I have a UIWebView that is called when a user presses a button. Everything works great, unless you account for behavior when the phone is without a data connection.

When attempting to open the UIWebView for the first time with the phone on airplane mode or without a data connection:

  • UIWebView starts to load
  • Fails to load

Once the UIWebView is allocated and fails to load for the first time, hitting a refresh button linked to an IBAction (that calls [webView reload]) causes:

  • Regardless of whether or not the phone has a data connection after the initial load fails, the refresh IBAction is called, but my NSLogs tell me neither webViewDidStartLoad, webViewDidFinishLoad, nor webView…didFailLoadWithError are called.

If I leave the app running and turn on a data connection to the device (e.g. turn off airplane mode,) then go back to my app and hit the refresh button, [webView reload] is called, but webViewDidStartLoad (and thus webViewDidFinishLoad) is not called.

Here's the relevant code:

To bring up the webView:

-(IBAction)loadWebView {

    webView = [[UIWebView alloc] init];
    webView.scalesPageToFit = YES;
    [webView setDelegate:self];

    NSString *urlAddress = @"http://google.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [webView loadRequest:requestObj];

}

Reload:

  -(IBAction)reloadWebView{
    [webView reload];
    NSLog(@"RELOAD");
}

webViewDidStart, DidFinish, didFailLoadWithError:

 - (void)webViewDidStartLoad:(UIWebView *)webView
{  
    NSLog(@"START LOAD");   
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
     NSLog(@"FINISH LOAD");    
}

- (void)webView:(UIWebView *) webView didFailLoadWithError:(NSError *)error 
{ 
       NSLog(@"DID FAIL");
}

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情场扛把子 2024-12-10 03:01:38

检查您是否应该尝试加载网页的一个好方法是使用可达性

NetworkStatus currentStatus = [[Reachability reachabilityForInternetConnection] 
                               currentReachabilityStatus];

if(currentStatus == kReachableViaWWAN) // 3G

else if(currentStatus == kReachableViaWifi) // ...wifi

else if(currentStatus == kNotReachable) // no connection currently possible

如果您在无法连接时甚至不启动 webView 加载,则可能可以完全避免这种情况。

不过,对奇怪行为的修复:在刷新方法中尝试检查它是否加载了任何内容,如果没有,则在那里调用 loadRequest:requestObj 。例如:

- (IBAction)reloadWebView {
    if(webView.request != nil)
        [webView reload];
    else {
        // reconstruct requestObj here, or use a class member
        [webView loadRequest:requestObj];
    }

    NSLog(@"RELOAD");
}

A good way to check if you should even bother trying to load a webpage is with Reachability.

NetworkStatus currentStatus = [[Reachability reachabilityForInternetConnection] 
                               currentReachabilityStatus];

if(currentStatus == kReachableViaWWAN) // 3G

else if(currentStatus == kReachableViaWifi) // ...wifi

else if(currentStatus == kNotReachable) // no connection currently possible

If you don't even start the webView loading when no connection is possible, you can probably avoid this scenario altogether.

A fix for the odd behaviour, though: In your refresh method try checking if it's loaded anything at all yet, and if not then call loadRequest:requestObj there. For example:

- (IBAction)reloadWebView {
    if(webView.request != nil)
        [webView reload];
    else {
        // reconstruct requestObj here, or use a class member
        [webView loadRequest:requestObj];
    }

    NSLog(@"RELOAD");
}
你另情深 2024-12-10 03:01:38

我确实遇到了同样的问题

  1. 在手机处于飞行模式或没有数据连接的情况下第一次打开我的UIWebView,依次调用了以下方法:
    • -webView:shouldStartLoadWithRequest:navigationType:
    • -webViewDidStartLoad:
    • -webView:didFailLoadWithError
  2. 然后,我点击了刷新按钮,也就是说,我调用了UIWebView-reload方法,没有调用委托方法,所以我什至无法收到超时通知。

我的解决方案是:

  • 在加载或重新加载请求之前检查网络可达性状态,非常感谢@darvids0n很好的建议(但我发现第一次请求失败后重新加载时request不是nil)。
  • 使用 loadRequest: 重新加载当前 URL,而不是 reload
  • 实现我自己的超时机制,例如,重新加载/加载请求后20秒,如果没有调用委托方法,我将其视为超时,并取消请求和loadingHUD动画。

其他一些类似问题

I did encounter the same problem:

  1. Open my UIWebView for the first time with the phone on airplane mode or without a data connection, the following methods were called in turn:
    • -webView:shouldStartLoadWithRequest:navigationType:
    • -webViewDidStartLoad:
    • -webView:didFailLoadWithError
  2. Then, I tapped refresh button, that's to say, I called UIWebView's -reload method, no delegate method was called, so I even couldn't get a timeout notification.

My solution is:

  • Check network reachability status before load or reload requests, many thanks to @darvids0n's great advice(But I found out the request is not nil when reload after the request failed for the 1st time).
  • Use loadRequest: to reload the current URL instead of reload.
  • Implement my own timeout mechanism, such as, 20s after reload/load request, if no delegate methods were called, I take it as a timeout, and cancel requests and loadingHUD animation.

Some other similar issues:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文