last day (15 days later) » 

10:47
1
Q: Validate URL without scheme

NephSwift 5, Xcode 10, iOS 12 My code uses UIApplication.shared.canOpenURL to validate URLs, which unfortunately fails without e.g. "http://". Example: print(UIApplication.shared.canOpenURL(URL(string: "stackoverflow.com")!)) //false print(UIApplication.shared.canOpenURL(URL(string: "http://stacko...

Why don't you append the scheme if not exists in the URL ?
@TheTiger I'm using a library for sockets and at least one of them isn't able to connect if there's a scheme.
@JoakimDanielson I don't. I don't want to check if the URL leads to an actual server that can be accessed, I just need some type of check if the URL is valid theoretically. So e.g. "bla" or "---" won't be valid but "bla.com" will.
@Neph Suppose http://190.128.0.1 exists and ftp://190.128.0.1 doesn't. In that case is 190.128.0.1 valid or invalid ?
See the huge list of URL Schemes so you can't just guess the valid one.
@TheTiger Theoretically it's a valid IP. It doesn't matter if it actually exists, I just want to check if it uses the right syntax. I found a couple of regex codes for that (e.g. this one) but they don't support all the cases I have to check.
@Neph Then you need to validate URL with regex not with if it can be open or not. Like for email we check if its valid or not but not if its reachable or not.
[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$ I found this regex for you which validates like this.
Hi Neph
10:53
I just tested it with "http://thisisalongtest.com" - this website doesn't exist and yet UIApplication.shared.canOpenURL(URL(string: "http://thisisalongtest.com")!) returns true. It looks like this function is performing checks on the validity of the URL (otherwise e.g. "bla" wouldn't fail, even though "bla.com" exists) and these checks I want to use - but with an empty scheme (so e.g. "127.0.0.1" is seen as valid too).
May be
The regex you mentioned in the comment box is using schemes as prefix
You just need to remove the regex for scheme
Did you try with regex I mentioned in comment?
The apple documentation for canOpenURL says: Returns a Boolean value indicating whether an app is available to handle a URL scheme. - so it's not checking if it's reachable but if it's valid.
No, haven't tested the refex yet.
But logically without a scheme URL is not valid. It just a string.
Just try with that regex. It should work for you.
That's why my question in the first post is: How do I add a "there's no scheme" scheme, so valid URLs like "stackoverflow.com" return true too (is this even possible?)?
If you want to add a valid scheme to the URL then this is not possible
10:59
If it's not possible to add an empty scheme, that's fine and I'll just continue adding a "http://" to an URL/IP if there isn't one, then test it with canOpenURL.
If you just want to validate the URL without scheme then I have shared the regex
Because no one knows which scheme is valid for which url
So, to be clear: It's not possible to add a "" scheme?
Yes, Not possible to add valid scheme.
I'm wonder how will you do that?
It doesn't matter what scheme is used for what URL. ;)
@TheTiger I don't know how to add an empty scheme, that's why I created the post.
@Neph I'm quoting this
If it's not possible to add an empty scheme, that's fine and I'll just continue adding a "http://" to an URL/IP if there isn't one, then test it with canOpenURL.
11:05
Easy: if !myurl.hasPrefix("http://") || !myurl.hasPrefix("https://") || !myurl.hasPrefix("ftp://") {tempurl = "http://" + myurl} - then just check with canOpenURL.
and it will make mailto://[email protected] to http://mailto://[email protected]
If you have static data and limited to only two schemes then you can use above condition
Otherwise it is wrong
Just tested it with your suggestion: I had to add more "\" to escape the existing ones but the change code doesn't work: "[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$", error: Can't do regex matching, reason: Can't open pattern U_REGEX_MISSING_CLOSE_BRACKET
I tested it on regextester.com/94502 site
@TheTiger I'm not using mailto://, only http://, ftp:// and maybe (only maybe, just to be sure) https://.
there might be some difference for iOS platform
11:15
Xcode was only complaining about the "\", which I escaped with "\\" but it doesn't check the actual regex syntax.
Okay I will check
Thanks.
^(https?://)?(www\\.)?([-a-z0-9]{1,63}\\.)*?[a-z0-9][-a-z0-9]{0,61}[a-z0-9]\\.[a-z]{2,6}(/[-\\w@\\+\\.~#\\?&/=%]*)?$

Please try by removing https and www from here .. this is your regex.

([-a-z0-9]{1,63}\\.)*?[a-z0-9][-a-z0-9]{0,61}[a-z0-9]\\.[a-z]{2,6}(/[-\\w@\\+\\.~#\\?&/=%]*)?$
I already tested this with the "https" part. Without it returns false for "http://stackoverflow.com", "https://stackoverflow.com", "ftp://127.0.0.1" and "127.0.0.1". Only the format "stackoverflow.com" seems to be okay.
11:36
Damn, UIApplication.shared.canOpenURL(URL(string: "http://---")!) returns true too, so much about that.
hmm
extension String {
func isValidUrl() -> Bool {
let regex = "((http|https|ftp)://)?((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+"
let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
return predicate.evaluate(with: self)
}
}
I modified the regex
print("http://stackoverflow.com".isValidUrl())
print("stackoverflow.com".isValidUrl())
print("ftp://127.0.0.1".isValidUrl())
print("www.google.com".isValidUrl())
print("127.0.0.1".isValidUrl())
print("127".isValidUrl())
print("hello".isValidUrl())
**Output**

true
true
true
true
true
false
false
Not sure if you required more cases or thats enough
100% regex is not possible to validate the email and url
Thanks, I'm going to try it. Btw, I don't want to validate e-mail addresses, only URLs/IPs. ;)
I know, I'm just telling ;)
11:54
"10.1..1.9" returns true but the other cases with and without scheme work, which is the most important part. Thanks! Want to post it as an alternative answer?
I posted it as an answer

last day (15 days later) »