IOS判断是否是URL

使用正则表达式判断

如果用户输入没有http等抬头,自动替补全

- (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 = @"(https|http|ftp|rtsp|igmp|file|rtspt|rtspu)://((((25[0-5]|2[0-4]\\d|1?\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1?\\d?\\d))|([0-9a-z_!~*'()-]*\\.?))([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\.([a-z]{2,6})(:[0-9]{1,4})?([a-zA-Z/?_=]*)\\.\\w{1,5}";

    NSPredicate* urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegex];

    return [urlTest evaluateWithObject:url];

}

 

你可能感兴趣的:(iOS,杂七杂八)