Check if valid url so i can pass it to an NSURL?

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(@"not valid");

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

host tests the domain of the URL, e.g. or

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).

Edit: the below answer is not true. (Apple docs:

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.


NSURL *url = [NSURL URLWithString:urlString];
if(url){ NSLog("valid"); }