URL Verification (Objective-C)

I am trying to verify the URL using this method:

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

He does not work. I think the problem is regular expression.

+6
source share
3 answers

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

Use if (URL && URL.scheme && URL.host) to check the URL.

+13
source

Try with it ..

 - (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 can help you.

+7
source
 NSString *urlRegEx = @"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"; 
+1
source

Source: https://habr.com/ru/post/902086/


All Articles