【问题标题】:Is didFailLoadWithError normally called after I return NO in the delegate function shouldStartLoadWithRequest?在委托函数 shouldStartLoadWithRequest 中返回 NO 后是否正常调用 didFailLoadWithError?
【发布时间】:2011-10-25 18:57:00
【问题描述】:

我看到 Apple 的 UIWebViewDelegate 中没有记录这种行为:

如果我向委托函数返回 NO

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

立即调用此函数并返回错误 101(无法加载此 URL。)。

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

既然我明确取消了请求,那么调用didFailLoadWithError是否正常?

【问题讨论】:

    标签: iphone objective-c uiwebview uiwebviewdelegate


    【解决方案1】:

    更新:如果您手动取消连接,UIWebView 将不会调用 didFailLoadWithError。

    我已经通过测试验证了这一点。 didFailLoadWithError 的原因是与 UIWebView 完全无关的其他事情。

    【讨论】:

    • 这是我的问题。返回 NO 实际上并没有被调用。之前的事情引发了一个异常,然后:WebKit 在 webView:decidePolicyForNavigationAction:request:frame:decisionListener:delegate: ... 无法识别的选择器中丢弃了一个未捕获的异常发送到实例 0x866c490
    【解决方案2】:

    你应该总是返回noshouldStartLoadWithRequest,如果网络视图应该开始加载内容,则返回YES;否则,

    如果您的连接出现错误 101 (net::ERR_CONNECTION_RESET): Unknown 错误,那么您可能遇到以下问题:

    You are trying to connect to the internet but the pages won’t load.
    You are trying to refresh/reload the page so many times but to no avail.
    You can see a lot of weird stuff all over the page. (links and images are unaligned.)
    When the page doesn’t load, you can see the phrase “Error 101 (net::ERR_CONNECTION_RESET): Unknown error”.
    

    您应该始终将请求处理给您的委托并返回 NO 然后在收到的来自 NSURLConnection 的响应调用中取消连接,如果一切正常(检查响应)在 web 视图中再次加载 urlrequest。再次加载 urlrequest 时,请确保在上述调用中返回 YES。 或者您可以使用同步或异步方法来处理它。

    【讨论】:

    • 我已经知道了。我在问是否将 NO 返回到 shouldStartLoadWithRequest 会导致调用 didFailLoadWithError。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多