0

I use WKWebview to load a URL.

let webView = WKWebview()
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url!)
webView.loadRequest(request)

How can I detect if the link the webView should load is broken?

Michael
  • 32,527
  • 49
  • 210
  • 370

4 Answers4

1

You can use canOpenUrl method:

UIApplication.sharedApplication().canOpenURL(url)

It will do the url validation and if the link is ok it returns true. It's mostly use before you call:

UIApplication.sharedApplication().openURL(url) 

to make sure this link can be open in safari but it should help you here too.

Make sure the link starts with http:// or https://.

Edited:

It will just check is the link is a correct url.

If you want to see the page is offline, authorisation issues, etc. you can implement WKNavigationDelegate protocol and check out this method:

- webView:didFailNavigation:withError:

this should give you more info.

It's always good idea to use: str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAl‌​lowedCharacterSet())! it make sure that you don't pass a character which are not allowed in URL.

Edited 2: To detect the status code you can try to implement:

- webView:decidePolicyForNavigationResponse:decisionHandler:

the navigation response is an NSURLResponse instance but whenever you make an HTTP request, the NSURLResponse object you get back is actually an instance of the NSHTTPURLResponse class so you should cast it to NSHTTPURLResponse. That should give you a statusCode. In the last line in the method you should call handler, for example decisionHandler(WKNavigationResponsePolicyAllow).

Greg
  • 25,317
  • 6
  • 53
  • 62
  • Assume the link is a correct url. The target page could be offline. How do I check that? – Michael Oct 26 '15 at 09:48
  • would you also suggest encoding each url before calling like it is suggested here: http://stackoverflow.com/questions/24551816/swift-encode-url/24552028#24552028 – Michael Oct 26 '15 at 11:53
  • @confile yes I wold. You don't want to pass a character which are not allowed in URL. I would use: str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! – Greg Oct 26 '15 at 12:05
  • Could you please add this to your answer. – Michael Oct 26 '15 at 12:24
  • But what if the server respond with 404 how do I detect this? – Michael Oct 26 '15 at 13:06
  • @confile check out the error in - webView:didFailNavigation:withError: method. – Greg Oct 26 '15 at 13:09
  • 1
    When I get a 404 there is no error and webView:didFailNavigation:withError is not called because of the redirect. – Michael Oct 26 '15 at 13:11
  • @confile please see edited code. I haven't tried this but I believe that should work. Let me know if it's not so I will remove my answer. – Greg Oct 26 '15 at 14:00
1

Ref: Answer exists here

if let url = NSURL(string: yourUrlString) {
    var canOpen = UIApplication.sharedApplication().canOpenURL(url)
 }
Community
  • 1
  • 1
arled
  • 2,577
  • 6
  • 27
  • 49
0

If you want to check if url string is correct and valid - just create NSURL object, if path contains error it will cast to nil:

let string = "http://google.com"
let url = NSURL(string: string)

let brokenString = "http:\\brokenurl.12"
let brokenUrl = NSURL(string: brokenString) // nil - it's not valid!
katleta3000
  • 2,484
  • 1
  • 18
  • 23
0

If you have implemented NSURLConnectionDelegate then below solution can be used.

didFailWithError method of NSURLConnectionDelegate can be used for this.

This method get called if an error occurs during the loading of a resource.

Can refer to below link https://developer.apple.com/documentation/foundation/nsurlconnectiondelegate/1418443-connection

Vikram Pote
  • 5,433
  • 4
  • 33
  • 37