//思路:传入一个请求的URL,进行网络请求,如果返回失败信息则说明此URL不可用
//1.首先进行第一步判断传入的字符串是否符合HTTP路径的语法规则,即”HTTPS://” 或 “HTTP://” ,从封装的一个函数,传入即可判断
- (NSURL *)smartURLForString:(NSString *)str {
NSLog(@"str:%@",str);
NSURL * result;
NSString * trimmedStr;
NSRange schemeMarkerRange;
NSString * scheme;
assert(str != nil);
result = nil;
trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ( (trimmedStr != nil) && (trimmedStr.length != 0) ) {
schemeMarkerRange = [trimmedStr rangeOfString:@"://"];
if (schemeMarkerRange.location == NSNotFound) {
result = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", trimmedStr]];
} else {
scheme = [trimmedStr substringWithRange:NSMakeRange(0, schemeMarkerRange.location)];
assert(scheme != nil);
if ( ([scheme compare:@"http" options:NSCaseInsensitiveSearch] == NSOrderedSame)
|| ([scheme compare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
result = [NSURL URLWithString:trimmedStr];
// NSLog(@"result:%@",result);
} else {
// result = [NSURL URLWithString:@""];
// 格式不符合
// NSLog(@"result:%@",result);
}
}
}
return result;
}
转载于:https://my.oschina.net/wenchengxu123/blog/1537696