我有以下代码
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"];
将导致一个空白页面,并且没有调用委托(delegate)方法。我该如何处理检测到此错误? (我已经检查过这个 iPhone SDK: Check validity of URLs with NSURL not working? 但我想知道是否有比正则表达式更好的东西?)
最佳答案
你可以尝试看看是否可以这样请求url:
NSURL *nsurl = [NSURL urlWithString:@"nacho"];
if([NSURLConnection canHandleRequest:[NSURLRequest requestWithURL: nsurl]]){
NSLog(@"URL can be loaded");
}else{
NSLog(@"URL cannot be loaded");
}
关于ios - 检测 WKWebview 何时无法处理请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897337/