【发布时间】:2015-12-10 03:21:01
【问题描述】:
我正在使用 UIWebView 制作一个简单的网络浏览器。用户在地址栏输入地址 -> 检查它。
1.如果文本是url -> 加载请求
2.如果文本是字符串 -> 执行谷歌搜索
第一种情况,如果string格式为:abc.xyz,如何添加scheme和host? 示例:用户输入 google.com -> 正确为 https://google.com engadget.com -> https://www.engadget.com.
我的问题是如何知道哪个部分必须添加到 url(http、https、有或没有 www)。
更新:
使用 NSURLSeassion 测试连接
- (void)checkRequest:(NSString*)urlRequest
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlRequest]];
[request setHTTPMethod:@"HEAD"];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode == 200)
NSLog(@"Correct url");
// check status code here
}
if (error) {
// handle other errors here
}
// handle data here
}];
[task resume];
}
更新 2
不需要检查url,添加http://方案,网站会自动重定向到正确的目的地。
【问题讨论】:
标签: ios objective-c uiwebview