美文网首页
ios判断字符串是不是url

ios判断字符串是不是url

作者: shuaikun | 来源:发表于2017-09-07 14:55 被阅读1176次
    判断当前的字符串是不是url
    #import "NSString+JudgeUrl.h"  
    @implementation NSString (JudgeUrl)
    - (BOOL)isUrl {
        
        if(self == nil) {
            return NO;
        }
        
        NSString *url;
        if (self.length>4 && [[self substringToIndex:4] isEqualToString:@"www."]) {
            url = [NSString stringWithFormat:@"http://%@",self];
        }else{
            url = self;
        }
        NSString *urlRegex = @"\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";
        NSPredicate* urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegex];
        return [urlTest evaluateWithObject:url];
    }
    @end
    

    相关文章

      网友评论

          本文标题:ios判断字符串是不是url

          本文链接:https://www.haomeiwen.com/subject/agsijxtx.html