Help me to write the code like "if my string is a valid URL do smth" Is it possible to write this in a couple strings of code?
5 Answers
I will assume that by URL, you are referring to a string identifying a internet resource location.
If you have an idea about the format of the input string , then why not manually check if the string starts with http://
, https://
or any other scheme you need. If you expect other protocols, you can also add them to the check list (e.g. ftp://
, mailto://
, etc)
if ([myString hasPrefix:@"http://"] || [myString hasPrefix:@"https://"])
{
// do something
}
If you are looking for a more solid solution and detect any kind of URL scheme, then you should use a regular expression.
As a side note, the NSURL class is designed to express any kind of resource location (not just internet resources). That is why, strings like img/demo.jpg
or file://bla/bla/bla/demo.jpg
can be transformed into NSURL objects.
However, according to the documentation the [NSURL URLWithString]
should return nil if the input string is not a valid internet resource string. In practice it doesn't.

- 6,353
- 4
- 35
- 64
+ (BOOL)validateUrlString:(NSString*)urlString
{
if (!urlString)
{
return NO;
}
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSRange urlStringRange = NSMakeRange(0, [urlString length]);
NSMatchingOptions matchingOptions = 0;
if (1 != [linkDetector numberOfMatchesInString:urlString options:matchingOptions range:urlStringRange])
{
return NO;
}
NSTextCheckingResult *checkingResult = [linkDetector firstMatchInString:urlString options:matchingOptions range:urlStringRange];
return checkingResult.resultType == NSTextCheckingTypeLink
&& NSEqualRanges(checkingResult.range, urlStringRange);
}

- 91
- 1
- 4
I used this solution which is apparently a better and less complex check than a Regex check -
- (BOOL)isURL:(NSString *)inputString
{
NSURL *candidateURL = [NSURL URLWithString:inputString];
return candidateURL && candidateURL.scheme && candidateURL.host;
}

- 12,529
- 9
- 59
- 94
-
That method will accept things like `""` or `"img/demo.jpg"` as @andrei-stanescu pointed out, and it's probably not what the OP had in mind. – mrgrieves Aug 19 '21 at 23:04
Try to create NSUrl with it, and see if it returns non-nil result.

- 13,706
- 1
- 34
- 48
-
I've already tried that: if ([NSURL urlWithString:urlString] != nil){ } but for some reason it just returns me my string!! (Though the method description say that it must return nil if string is not a valid url..don't really know why) – Stas Feb 13 '12 at 13:08
if ([NSURL URLWithString:text]) {
// valid URL
}
else {
// invalid URL
}

- 51,577
- 12
- 107
- 152
-
that's the point it doesn't work...have I met a strange bug or what??? I have a log in the if NSLog(@"invalid url:%@", [NSURL URLWithString:urlString]); And it shows me: invalid url:img/playgrounds.png – Stas Feb 13 '12 at 13:22
-
3This is why you should say what you've already tried in your question. If your string is literally "img/playgrounds.png" then it is correct; that's not a valid URL. If you try it with, say, `http://img.ly/playground.png` then it will work. – Stephen Darlington Feb 13 '12 at 13:49