在委托函数shouldStartLoadWithRequest中返回NO后,didFailLoadWithError通常会被调用吗?
我看到 Apple 的 UIWebViewDelegate 中未记录此行为:
如果我向委托函数返回 NO,则
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
立即调用此函数,并显示错误 101(无法加载此 URL。)。
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
由于我明确取消了请求,调用 didFailLoadWithError 是否正常?
I am seeing this behavior not documented in Apple's UIWebViewDelegate:
If I return NO to the delegate function
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
This function is immediately called with the error 101 (This URL cannot be loaded.).
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
Since I explicitly cancelled the request, is it even normal for didFailLoadWithError to be invoked?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:如果您手动取消连接,UIWebView 将不会调用 didFailLoadWithError。
我已经通过测试验证了这一点。 didFailLoadWithError 的原因是与 UIWebView 完全无关的其他事情。
Update: UIWebView will NOT call didFailLoadWithError if you manually cancel the connection.
I've verified this with testing. The reason why didFailLoadWithError is for something else completely unrelated to UIWebView.
您应该始终返回
no
shouldStartLoadWithRequest,如果 Web 视图应该开始加载内容,则返回YES
;否则,NO如果您的连接出现 Error 101 (net::ERR_CONNECTION_RESET): Unknown error,那么您可能遇到这些情况:
您应该始终将请求处理到您的委托中并返回 NO 然后在收到的请求中来自 NSURLConnection 的响应调用取消连接,如果一切正常(检查响应),则在 Web 视图中再次加载 urlrequest。确保再次加载 urlrequest 时在上述调用中返回 YES。
或者您可以使用同步或异步方法来处理它。
You should always return
no
shouldStartLoadWithRequest, returnYES
if the web view should begin loading content; otherwise, NOIf your connection has Error 101 (net::ERR_CONNECTION_RESET): Unknown error, then you are maybe experiencing these:
You should always handle the request in to your delegate and return NO Then in the received response call from NSURLConnection cancel the connection and if everything is fine (check response) load the urlrequest once more in the webview. Make sure to return YES in the above call when loading the urlrequest again.
or you can handle it with use of the Synchronous or Asynchronous methods.