2
votes

I have the following code

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;

And the delegate:

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

Works fine and I can catch errors when the could not be loaded (404, etc). But how do I catch the error when the nsurl was something WKWebView cannot even handle?

For example:

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

will lead to a blank page with no delegate method being called. How can I handle detect this error? (I have checked at this iPhone SDK: Check validity of URLs with NSURL not working? but I wonder if there is anything better than regex?)

1

1 Answers

2
votes

You can try and see if the url can be requested like this:

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

if([NSURLConnection canHandleRequest:[NSURLRequest requestWithURL: nsurl]]){
    NSLog(@"URL can be loaded");
}else{
    NSLog(@"URL cannot be loaded");
}