ios - 检测 WKWebview 何时无法处理请求

标签 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"];

将导致一个空白页面,并且没有调用委托(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/

相关文章:

iphone - 我应该在为其分配新值之前释放 NSString 吗?

xcode - QuickLook 插件是 "damaged/missing resources"。为什么?

cocoa - iPad 方向问题

swift - 无法使用列表类型 NSURL 调用加载数据...正在加载本地 Web View 内容

swift - 从服务器检索数据时,闭包到底做了什么?

iphone - URLWithString 对于资源路径返回 nil - iphone

ios - 使用Parse作为我的BaaS制作社交照片共享应用程序

iphone - UIPickerView 不更新

c# - 在默认应用程序中打开文件

iphone - 查看 MPMoviePlayerController 当前是否正在播放