URL Validation (Objective-C)

前端 未结 4 1215
猫巷女王i
猫巷女王i 2020-12-20 21:23

I am trying to validate a URL with this method:

Code:

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

    NSString *urlRegEx=
    @\"((https?|ftp|gop         


        
相关标签:
4条回答
  • 2020-12-20 21:25

    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.

    0 讨论(0)
  • 2020-12-20 21:29

    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

    0 讨论(0)
  • 2020-12-20 21:39

    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.

    0 讨论(0)
  • 2020-12-20 21:44
    NSString *urlRegEx = @"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
    
    0 讨论(0)
提交回复
热议问题