我有以下代码
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)webViewWKWebView *)webView
decidePolicyForNavigationResponseWKNavigationResponse *)navigationResponse
decisionHandlervoid (^)(WKNavigationResponsePolicy))decisionHandler {
...
}
工作正常,当无法加载(404 等)时,我可以捕获错误。但是当 nsurl 是 WKWebView 甚至无法处理的东西时,我该如何捕捉错误?
例如:
NSURL *nsurl = [NSURL urlWithString"nacho"];
将导致没有调用委托(delegate)方法的空白页面。我该如何处理检测到这个错误? (我已经检查过这个 iPhone SDK: Check validity of URLs with NSURL not working? 但我想知道有没有比正则表达式更好的东西?)
Best Answer-推荐答案 strong>
你可以试试看是否可以这样请求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/
|