判断字符串是否是URL
2017-04-28 本文已影响0人
天码行空
- (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];
}