Swift 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://stackoverflow.com")!)) //true
print(UIApplication.shared.canOpenURL(URL(string: "129.0.0.1")!)) //false
print(UIApplication.shared.canOpenURL(URL(string: "ftp://129.0.0.1")!)) //true
I'm aware of the change with schemes (iOS9+) and I know that I can just add a prefix like "http://" if the String doesn't start with it already, then check this new String but I'm still wondering:
Question: How do I add a "there's no scheme" scheme, so valid URLs like "stackoverflow.com" return true too (is this even possible?)?
It's not possible to add a valid scheme to
URLbecause no one knows which prefix will be add to whichURL. You can just validate aURLwith the help ofregex.I searched and modified the regex.
I tested it with below urls:
Note: 100% regex is not possible to validate the
emailandurl