1

Possible Duplicate:
How to validate an url on the iPhone

In Objective-C, does anyone have a good method to test if a given string appears to be a URL?

Community
  • 1
  • 1
CodeGuy
  • 28,427
  • 76
  • 200
  • 317

2 Answers2

25

Do this:

NSURL* url = [NSURL URLWithString:stringToTest];
if (url && url.scheme && url.host)//This comparision never fails
 {
  //the url is ok
  NSLog(@"%@ is a valid URL", yourUrlString);
 }

If stringToTest is indeed an URL then url will be instantiate as expected. Otherwise +[NSURL URLWithString:] return nil.

Most methods in Cocoa Touch return nil on illegal input, very few actually throws an NSInvalidArgumentException. Each method is documented with what they return on invalid input.

Prasanna
  • 945
  • 10
  • 24
PeyloW
  • 36,742
  • 12
  • 80
  • 99
  • Nice. Never would have thought to use NSURL to validate a url string. – Dan Ray Aug 10 '11 at 20:43
  • 2
    Just remember that this will validate ANY URL. So even if it is a file URL or a web URL, they will both validate to true with this form of checking. PeyloW's answer is right, I just wanted to let you know that web URL's aren't the only type NSURL accepts. – Lucas Derraugh Aug 10 '11 at 20:46
  • 2
    Also note that it validates partial and relative URLs. So checking of the schema and/or domain is there can also be a good practice. – PeyloW Aug 11 '11 at 08:59
  • yeah "asdf.com" is considered a valid URL so you'll want to check for a scheme. – drewish Dec 12 '11 at 05:06
  • 2
    this actually doesn't work. if the string is blank or you just enter anything into the string "asdf" if url will not be nil and thus pass this test. – Bot Mar 26 '13 at 20:32
  • 3
    Yup - doesn't work for a web url on xcode 4.6.3 and iOS 6. This code example just returns whatever the string is as url -I can just put my name, simon, as the stringToTest and url ends up with the value "simon" – SimonTheDiver Jun 27 '13 at 16:54
  • 1
    Its not worked WOrking below steps.. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; return [NSURLConnection canHandleRequest:request]; – Mayank Purwar Apr 17 '15 at 09:37
  • this does not work. What works is the answer given in comments by @MayankPurwar – Ahsan Ebrahim Khatri Dec 02 '15 at 07:18
  • better implementation to check host and scheme : http://stackoverflow.com/a/5081447/1586606 – Ammar Mujeeb Oct 19 '16 at 06:36
-2

You can use a regular expression. For iPhone 3 and up, you can do it without a framework. Otherwise use RegexKitLite or something.

Here is a regex pattern for checking URLs:

"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+"

Doing it without a framework:

- (BOOL)validateUrl:(NSString *)candidate {
    NSString *urlRegEx =
    @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
    return [urlTest evaluateWithObject:candidate];
}
mattacular
  • 1,849
  • 13
  • 17
  • 3
    Use `NSURL` as described by PeyloW. NSURL is designed to handle the full URL standard; best to not re-invent the wheel. – bbum Aug 10 '11 at 20:13
  • Truth^^ I forgot about NSUrl. BUT if for some reason you wanted to relax (or make stricter, NSUrl will accept prefixes other than http and https) the validation standard you might still want to be able to define your own pattern. – mattacular Aug 10 '11 at 21:17
  • Yah -- but you'd probably want to start with a [likely considerably more complex] an expression that accepts the standard, then back off to support the non-standard URLs. Or, better, fix the source of the invalid URLs. :) – bbum Aug 10 '11 at 21:55
  • Hm. What if it starts with www, like www.google.com? – CodeGuy Aug 11 '11 at 17:00
  • You can try the pattern above minus the (http|https):// part: "((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+" but like bbum suggested it might be better to force your users to provide a valid URL with the prefix and everything – mattacular Aug 11 '11 at 18:02
  • A better , cleaner and efficient solution for checking whether a URL is valid or not. NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yourstring"]]; bool valid = [NSURLConnection canHandleRequest:req]; Source : http://stackoverflow.com/a/13650542/2513947 Hope it helps! – Rizwan Ahmed May 25 '16 at 14:27