5

I'm trying to validate a url using NSURL but it doesn't work for me.

Example:

func verifyUrl (urlString: String?) -> Bool {
    if let urlString = urlString {
        if let _  = NSURL(string: urlString) {
            return true
        }
    }
    return false
}


let ex1 = "http://google.com"
let ex2 = "http://stackoverflow.com"
let ex3 = "Escolas" // Not a valid url, I think

verifyUrl(ex1) // true
verifyUrl(ex2) // true
verifyUrl(ex3) // true

I think that "Escolas" can't return true, what am I doing wrong?

Paulo
  • 1,458
  • 2
  • 12
  • 26
Gabriel Rodrigues
  • 510
  • 1
  • 8
  • 29
  • "Escolas" is indeed a valid URL. You may be looking for something like `if let url = NSURL(...) { return url.scheme == "http" || url.scheme == "https" }`. – Kevin Jan 27 '16 at 22:37
  • http://stackoverflow.com/a/34849186/2303865 – Leo Dabus Jan 27 '16 at 23:21

2 Answers2

9

I think what you're missing is open the url with UIApplication.sharedApplication().canOpenURL(url) for example, change your code to this one:

func verifyUrl (urlString: String?) -> Bool {
   if let urlString = urlString {
       if let url  = NSURL(string: urlString) {
           return UIApplication.sharedApplication().canOpenURL(url)
       }
   }
   return false
}

verifyUrl("escola") // false
verifyUrl("http://wwww.google.com") // true

The constructor of NSURL not validate the url as you think, according to Apple:

This method expects URLString to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding

I hope this help you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • `+1`I just change to this way and shows this message `-canOpenURL: failed for URL: "Escolas" - error: "Invalid input URL"` instead of return just false – Gabriel Rodrigues Jan 27 '16 at 22:40
  • @GabrielRodrigues The compiler output to the log what you said but the result is false in the function if you see it. – Victor Sigler Jan 27 '16 at 22:44
  • @GabrielRodrigues Remeber that there a lot of ways to solve the problem of validate an URL, using Regular Expression, using `NSDataDetector`, etc you can see more in my answer [here](http://stackoverflow.com/questions/9587458/using-nsregularexpression-to-extract-urls-on-the-iphone/29498086#29498086) but as you ask for using NSUrl I just post it in this way – Victor Sigler Jan 27 '16 at 22:47
  • Then the compiler tell me this message is not a problem ? – Gabriel Rodrigues Jan 27 '16 at 22:47
  • 2
    The more easy way of validate is using `NSDataDetector` as I posted in my answer in the comment http://stackoverflow.com/questions/9587458/using-nsregularexpression-to-extract-urls-on-the-iphone/29498086#29498086, please see it – Victor Sigler Jan 27 '16 at 22:49
0

[Swift 3.0]

Very similar but I used an extension on String to achieve the same result.

extension String {

    func isValidURL() -> Bool {

        if let url = URL(string: self) {

            return UIApplication.shared.canOpenURL(url)
        }

    return false 
    }
}
Edouard Barbier
  • 1,815
  • 2
  • 19
  • 32
  • Still get some errors in the console about that. Doesn't make much sense to me as you're unwrapping and checking that it's an instance of URL: -canOpenURL: failed for URL: "dddddddddd" - error: "Invalid input URL" – David Jarrin Apr 07 '20 at 12:32