[英]How do I properly implement didFailProvisionalNavigation with WKWebView?
I'm making a web browser and like every other web browser I'd like to inform the user of the error that occurred when a web page failed to load. 我正在创建一个Web浏览器,并且像其他所有Web浏览器一样,我想告知用户网页加载失败时发生的错误。
Currently, I'm displaying the localizedDescription of the error in the didFailProvisionalNavigation method. 目前,我正在显示在didFailProvisionalNavigation方法错误的localizedDescription。 That's all!
就这样!
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
[self presentError:error.localizedDescription animated:YES];
[_delegate webViewDidFailNavigation:self error:error];
}
The presentError method handles some custom UI stuff irrelevant to the question. presentError方法处理与问题无关的一些自定义UI内容。
The method above works excellent for displaying the usual errors such as: 上述方法非常适合显示常见错误,例如:
But it also displays some unusual errors such as: 但它也会显示一些不寻常的错误,例如:
I don't like this method for two reasons: 我不喜欢这种方法有两个原因:
I'm not aware of what other minor errors could occur that shouldn't be handled the way I'm handling them. 我不知道可能发生的其他小错误,不应该像我处理它们那样处理。 Unfortunately, I don't know of a way to distinguish between major errors such as "Connection appears to be offline" and minor errors such as "Operation couldn't be completed".
不幸的是,我不知道如何区分主要错误,如“连接似乎脱机”和轻微错误,如“操作无法完成”。
What is the standard way of handling navigation failures? 处理导航故障的标准方法是什么?
Instead of 代替
webView(_:didFailProvisionalNavigation:withError:)
why do you not use this? 你为什么不用这个?
webView(_:didFail:withError:)
It seems that should only be called when there is an error preventing the site from displaying content, not when a page is already rendered and trying to load the next page. 似乎只应在出现阻止网站显示内容的错误时调用,而不是在页面已经呈现并尝试加载下一页时调用。
I needed to use didFailProvisionalNavigation too. 我也需要使用didFailProvisionalNavigation。 For now I solved it like this:
现在我解决了这个问题:
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
{
if(error.code != -999) // Prevent the error from showing when didFailProvisionalNavigation is triggered after a cancelled load (reload)
{
// Show the error
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.