WKWebView - 当组件发生故障时未调用didFailProvisionalNavigation

rae*_*aed 11 objective-c uiwebview ios wkwebview

当页面组件加载WKWebView失败但didFailProvisionalNavigation未被调用时,我试图记录错误.

我从我的页面中删除了一个css文件来测试它,但没有结果.

当我在Google Chrome上加载页面时,我可以找到丢失的文件

在此输入图像描述

WKWebView初始化为:

@interface MyWebView : UIViewController <WKNavigationDelegate>
@property(strong,nonatomic) WKWebView *loginWebView;
@end
Run Code Online (Sandbox Code Playgroud)

.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];
}
Run Code Online (Sandbox Code Playgroud)

然后我实现了以下方法:

webView:decidePolicyForNavigationAction:decisionHandler:
webView:didStartProvisionalNavigation:
webView:didFinishNavigation:
webView:didFailNavigation:withError:
webView:didFailProvisionalNavigation:withError:
Run Code Online (Sandbox Code Playgroud)

调用前三个方法,但既不调用didFailNavigation也不didFailProvisionalNavigation调用

通过查看文件didFailProvisionalNavigation我们有

在Web视图加载内容时发生错误时调用.

好吧,内容失败了(css文件)并且没有调用该方法,我该怎么做?

PS:我也试过用UIWebView!

小智 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);
}

Run Code Online (Sandbox Code Playgroud)

这有效!