6

I am trying to validate a URL with this method:

Code:

- (BOOL) validateUrl: (NSString *) candidate {

    NSString *urlRegEx=
    @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)";

    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
    return [urlTest evaluateWithObject:candidate];

}

It does not function. I think the problem is with the regular expression.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
saleh Hosseinkahni
  • 497
  • 2
  • 6
  • 17

4 Answers4

13

You can use + (id)URLWithString:(NSString *)URLString method of NSURL, which returns nil if the string is malformed.

Use if (URL && URL.scheme && URL.host) for checking URL.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • 1
    i tried it! but still it does not work ! for example "ht://www.yahoo." this works well ! and it does not return nill !!! – saleh Hosseinkahni Nov 22 '11 at 10:41
  • 2
    This is valid URl according to RFC 2396.From apple documentation The string with which to initialize the NSURL object. Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808. use "if (URL && URL.scheme && URL.host)" for checking URL. – Parag Bafna Nov 22 '11 at 11:37
  • 3
    RFC 2396 uses this ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? regular expression. For more information refer http://www.ietf.org/rfc/rfc2396.txt – Parag Bafna Nov 22 '11 at 11:58
  • I'd do what what's suggested here and then match `[url scheme]` against a scheme whitelist. – Wevah Mar 04 '12 at 10:11
  • This is false! `[NSURL urlWithString:]` will return a non-nil URL regardless of the string passed. Try `[NSURL urlWithString:@"foo"]` if you're not convinced. – Sam Ballantyne Apr 09 '15 at 14:45
  • @SamBallantyne Read my all comments. – Parag Bafna Apr 10 '15 at 05:30
  • @ParagBafna The `URLWithString:` method of NSURL does not return nil if the string is "malformed". – Sam Ballantyne Apr 10 '15 at 15:12
  • @SamBallantyne From apple documentation : "return value: If the URL string was malformed or nil, returns nil." – Parag Bafna Apr 10 '15 at 18:26
6

Try with this..

- (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];
}

it may help u out.

iCoder4777
  • 1,682
  • 1
  • 14
  • 35
1

What about using the following:

NSURL * url = [NSURL URLWithString:@"http://www.google.com"];

BOOL isValid = [UIApplication.sharedApplication canOpenURL:url];

note: best practice is to use regex

Amr Angry
  • 3,711
  • 1
  • 45
  • 37
0
NSString *urlRegEx = @"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
  • Not the best regex. For example, doesn't work with Https://google.com because of the capital "H," even though this is a valid URL. – Joshua Wolff Jun 24 '19 at 04:44