In an iPhone app I am developing, there is a setting in which you can enter a URL, because of form & function this URL needs to be validated online as well as offline.
So far I haven't been able to find any method to validate the url, so the question is;
How do I validate an URL input on the iPhone (Objective-C) online as well as offline?
Why not instead simply rely on
Foundation.framework
?That does the job and does not require
RegexKit
:According to Apple documentation :
use this-
Some URL's without / at the end are not detected as the correct one in the solutions above. So this might be helpful.
I solved the problem using RegexKit, and build a quick regex to validate a URL;
Then I check if the matchedString is equal to the subjectString and if that is the case the url is valid :)
Correct me if my regex is wrong ;)
Selfishly, I would suggest using a
KSURLFormatter
instance to both validate input, and convert it to somethingNSURL
can handle.Instead of writing your own regular expressions, rely on Apple's. I have been using a category on
NSString
that usesNSDataDetector
to test for the presence of a link within a string. If the range of the link found byNSDataDetector
equals the length of the entire string, then it is a valid URL.