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(@"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).


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"); }