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