iPhone - UIWebView 在没有数据连接的情况下不调用 didFailLoadWithError
我已经搜索过,但没有找到解决方案,所以希望有人能提供帮助。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查您是否应该尝试加载网页的一个好方法是使用可达性。
如果您在无法连接时甚至不启动
webView
加载,则可能可以完全避免这种情况。不过,对奇怪行为的修复:在刷新方法中尝试检查它是否加载了任何内容,如果没有,则在那里调用
loadRequest:requestObj
。例如:A good way to check if you should even bother trying to load a webpage is with Reachability.
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:我确实遇到了同样的问题:
UIWebView
,依次调用了以下方法:-webView:shouldStartLoadWithRequest:navigationType:
-webViewDidStartLoad:
-webView:didFailLoadWithError
UIWebView
的-reload
方法,没有调用委托方法,所以我什至无法收到超时通知。我的解决方案是:
request
不是nil
)。loadRequest:
重新加载当前 URL,而不是reload
。其他一些类似问题:
I did encounter the same problem:
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
UIWebView
's-reload
method, no delegate method was called, so I even couldn't get a timeout notification.My solution is:
request
is notnil
when reload after the request failed for the 1st time).loadRequest:
to reload the current URL instead ofreload
.Some other similar issues: