+ (BOOL)validateUrl: (NSString *)candidate
{
NSString *candidateUrlString = candidate;
if (![candidate.lowercaseString hasPrefix:@"http://"] && ![candidate.lowercaseString hasPrefix:@"https://"]) {
candidateUrlString = [NSString stringWithFormat:@"http://%@", candidate];
}
NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidateUrlString];
}
验证URL格式的Objective-C方法
本文介绍了一个Objective-C中的方法,用于验证给定字符串是否为有效的URL。该方法首先检查URL是否包含'http://'或'https://'前缀,并在缺失时自动添加'http://'。接着使用正则表达式和NSPredicate来判断字符串是否符合URL的标准格式。
5976

被折叠的 条评论
为什么被折叠?



