【问题标题】:WKWebView - didFailProvisionalNavigation not called when a component failWKWebView - 组件失败时不调用 didFailProvisionalNavigation
【发布时间】:2017-05-01 00:43:10
【问题描述】:

WKWebView 中加载的页面组件失败但未调用didFailProvisionalNavigation 时,我正在尝试记录错误。

我从页面中删除了一个 css 文件以进行测试,但没有结果。

当我在谷歌浏览器上加载页面时,我可以找到丢失的文件

WKWebView 初始化为:

@interface MyWebView : UIViewController <WKNavigationDelegate>
@property(strong,nonatomic) WKWebView *loginWebView;
@end

.m 文件

@implementation MyWebView

- (void) initWebView {

    // Set and Load the WebView
    self.webView = [[WKWebView alloc] initWithFrame:self.view.frame];
    self.webView.navigationDelegate = self;
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
}

然后我实现了下面的方法:

webView:decidePolicyForNavigationAction:decisionHandler:
webView:didStartProvisionalNavigation:
webView:didFinishNavigation:
webView:didFailNavigation:withError:
webView:didFailProvisionalNavigation:withError:

前三个方法被调用,但didFailNavigationdidFailProvisionalNavigation 都没有被调用

通过查看documentation 对应的didFailProvisionalNavigation 我们有

当 Web 视图加载内容时发生错误时调用。

嗯,一个内容失败(css文件)并且方法没有被调用,我该怎么做呢?

PS:我也尝试过使用UIWebView

【问题讨论】:

  • 我面临着类似的问题。找到任何解决方案了吗?
  • 很遗憾没有:(
  • 哦 :-( 这不酷!如果我能找到任何解决方案,我会在这里发布。
  • 那会很酷,提前谢谢队友
  • 两年后有更新吗? ;)

标签: ios objective-c uiwebview wkwebview


【解决方案1】:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSInteger statusCode = ((NSHTTPURLResponse *)navigationResponse.response).statusCode;
    NSLog(@"statusCode:%ld", statusCode);
    if (statusCode/100 == 4 || statusCode/100 == 5) {
        NSLog(@"webview error:%@", navigationResponse.response);
    }
    decisionHandler(WKNavigationResponsePolicyAllow);
}

这行得通!

【讨论】:

    猜你喜欢
    • 2017-06-29
    • 2017-12-21
    • 2015-05-17
    • 2016-03-20
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2018-11-30
    相关资源
    最近更新 更多