ios判断字符串是不是url
2017-09-07 本文已影响1176人
shuaikun
判断当前的字符串是不是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