8

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?

Stas
  • 9,831
  • 9
  • 39
  • 76

5 Answers5

19

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.

Andrei Stanescu
  • 6,203
  • 4
  • 33
  • 60
7
+ (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);
}
MarkII
  • 91
  • 1
  • 4
7

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;
}
Nishant
  • 11,895
  • 9
  • 53
  • 89
1

Try to create NSUrl with it, and see if it returns non-nil result.

Nickolay Olshevsky
  • 12,356
  • 1
  • 28
  • 44
  • 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
0
if ([NSURL URLWithString:text]) {
  // valid URL
}
else {
  // invalid URL
}
Stephen Darlington
  • 49,617
  • 11
  • 101
  • 147
  • 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
  • 3
    This 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