12

I want to check if an NSString is a valid URL so I can parse it to an NSURL variable... is there an easy way to do this? :)

CRASH For some reason the app crashes when checking.....

NSURL *shortURL = [[NSURL alloc] initWithString:data];
if(shortURL == nil)
{
    NSLog(@"INVALID");
}
else {
    NSLog(@"COOOL");
}

The console gives me this error.....

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initWithString:relativeToURL:]: nil string parameter' 2010-03-01 19:24:14.797 Snippety[8289:5e3b] Stack: ( 8307803, 2419510843, 8391739, 8391578, 2898550, 3152497, 12262, 12183, 27646, 2662269, 2661144, 2454790485, 2454790162 )

3 Answers 3

17

I'm using the below method to check whether NSString testString is a valid URL:

NSURL *testURL = [NSURL URLWithString:testString];
if (testURL && [testURL scheme] && [testURL host])
{
    NSLog(@"valid");
}
{
    NSLog(@"not valid");
}

scheme tests the prefix of the URL, e.g. http://, https:// or ftp://

host tests the domain of the URL, e.g. google.com or images.google.com

Note: This will still give you some false positives, e.g. when checking http://google,com (note the comma) will return valid but it's definitely more precise than just checking whether NSURL is not nil ([NSURL urlWithString:@"banana"] is not nil).

1
  • there is an else missing
    – Tom
    Apr 14, 2021 at 11:20
11

Edit: the below answer is not true. (Apple docs: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/index.html)


NSURL's URLWithString returns nil if the URL passed is not valid. So, you can just check the return value to determine if the URL is valid.

Example:

NSURL *url = [NSURL URLWithString:urlString];
if(url){ NSLog("valid"); }
6
  • For some reason the app is crashing..... It works if the URL is valid, if not it just crashes ?? code above
    – tarnfeld
    Mar 1, 2010 at 19:19
  • Try URLWithString rather than initWithString
    – MarkPowell
    Mar 1, 2010 at 20:04
  • 2
    NSURL will gladly handle any string value except nil. That's the only one you need to special-case for May 14, 2012 at 14:43
  • 38
    this is wrong. any string will pass as a url when using URLWithString.
    – Bot
    Mar 26, 2013 at 20:33
  • 2
    Turns out NSURL's handling of nil strings changed fairly recently, for the release of OS X 10.7 etc.: mikeabdullah.net/urlwithstring-changed.html Jul 24, 2013 at 13:54
2

This is basically what the +[NSURLConnection canHandleRequest:] method does. This method runs a preflight check against an NSURLRequest to make sure the request is resolvable. To quickly check a particular URL, you can do this:

BOOL isValidURL = [NSURLConnection canHandleRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://example.com"]];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.