简体   繁体   中英

iOS - NSDataDetector: Why does url missing '.com' (http://google) return as valid URL?

I am trying to validate provided URLs from a text field using NSDataDetector and it always returns valid if I enter a URL containing "http://" in the front of any text.

i.e. http://someurl, http://someotherurl, etc.

FYI - Although web browsers are capable of interpretting urls using the above format, NSURL returns an error when using this format every time. Since I need to call an action with a provided URL in my app, it needs to be valid when creating an NSURL from the provided URL text.

Any ideas on how to correct this as those urls are not really valid?

Here is my code:

   linkDetector = NSDataDetector(types: NSTextCheckingType.Link.rawValue, error: nil)
    linkDetector?.enumerateMatchesInString(self.textField.text, options: NSMatchingOptions(0), range: NSMakeRange(0, countElements(urlString!)), usingBlock: { (result: NSTextCheckingResult!, flags, stop) -> Void in
            self.validatedUrlString = self.textField.text
    })

According to RFC 1304 Section 3.5 , a valid domain name expansion (after the http:// protocol specifier) could be as follows:

<domain> ::= <subdomain> | " "
<subdomain> ::= <label> | <subdomain> "." <label>
<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]

Essentially, this means that the . is not a requirement for a domain name. A very common example of this is http://localhost which contains no . .

Because there are new top level domains like .google and .apple so a URL consisting of just "apple" could actually be a valid URL these days.

And in fact, if I type in http://apple , that plain looking valid URL actually successfully resolves in Firefox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM