检测WKWebview何时无法处理请求

时间:2016-06-18 13:11:00

标签: ios cocoa nsurl nsurlrequest wkwebview

我有以下代码

NSURL *nsurl = [NSURL urlWithString:@"http://url.that.does.not.exist.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl];
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
WKWebView *wv = [[WKWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds] configuration:config];
wv.navigationDelegate = self;

代表:

- (void)webView:(WKWebView *)webView
     decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
                       decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
...
}

工作正常,我可以在无法加载时捕获错误(404等)。但是当nsurl是WKWebView甚至无法处理的东西时,如何捕获错误?

例如:

NSURL *nsurl = [NSURL urlWithString:@"nacho"];

将导致空白页面没有调用委托方法。如何处理检测到此错误? (我已经检查了iPhone SDK: Check validity of URLs with NSURL not working?,但我想知道是否有比正则表达更好的东西?)

1 个答案:

答案 0 :(得分:1)

您可以尝试查看是否可以像这样请求网址:

{{1}}