添加 http:// 到 NSURL 如果它不存在
Add http:// to NSURL if it's not there
我在我的应用程序中使用网络视图,从文本字段中获取 URL。如果字符串以 "http://" 开头,它会起作用。我正在尝试修改代码,以便它也可以处理用户不输入 "http://" 或 "https://"
的情况
如何检查 URL 中是否没有 "http://"?
如何修改URL在其中添加"http://"?
NSString *URLString = textField.text;
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
试试这个。
NSString *URLString = textField.text;
if ([URLString rangeOfString:@"http://"].location == NSNotFound && [URLString rangeOfString:@"https://"].location == NSNotFound)
{
URLString=[NSString stringWithFormat:@"http://%@",textField.text];
}
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
NSString *urlString = @"google.com";
NSURL *webpageUrl;
if ([urlString hasPrefix:@"http://"] || [urlString hasPrefix:@"https://"]) {
webpageUrl = [NSURL URLWithString:urlString];
} else {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", urlString]];
}
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:webpageUrl];
[self.myWebView loadRequest:urlRequest];
让我更新对 Swift 4 和 WKWebKit
的回答
var urlString = "www.apple.com"
if urlString.hasPrefix("https://") || urlString.hasPrefix("http://"){
let myURL = URL(string: urlString)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}else {
let correctedURL = "http://\(urlString)"
let myURL = URL(string: correctedURL)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
试试这个:
NSString *URL = @"apple.com" ;
NSURL *newURL ;
if ([URL hasPrefix:@"http://"] || [URL hasPrefix:@"https://"]) {
newURL = [NSURL URLWithString:URL] ;
}
else{
newURL = [NSURL URLWithString:[NSString
stringWithFormat:@"http://%@",URL]] ;
}
NSLog(@"New URL : %@",newURL) ;
我是这样解决的,Swift 4 及以上:
var yourString = "facebook.com"
var urlPath = ""
if yourString.contains(find: "https://") {
urlPath = path
} else {
let newPath = "https://\(path)"
urlPath = newPath
}
guard let url = URL(string: urlPath) else {return}
UIApplication.shared.open(url)
我在我的应用程序中使用网络视图,从文本字段中获取 URL。如果字符串以 "http://" 开头,它会起作用。我正在尝试修改代码,以便它也可以处理用户不输入 "http://" 或 "https://"
的情况如何检查 URL 中是否没有 "http://"? 如何修改URL在其中添加"http://"?
NSString *URLString = textField.text;
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
试试这个。
NSString *URLString = textField.text;
if ([URLString rangeOfString:@"http://"].location == NSNotFound && [URLString rangeOfString:@"https://"].location == NSNotFound)
{
URLString=[NSString stringWithFormat:@"http://%@",textField.text];
}
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
NSString *urlString = @"google.com";
NSURL *webpageUrl;
if ([urlString hasPrefix:@"http://"] || [urlString hasPrefix:@"https://"]) {
webpageUrl = [NSURL URLWithString:urlString];
} else {
webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", urlString]];
}
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:webpageUrl];
[self.myWebView loadRequest:urlRequest];
让我更新对 Swift 4 和 WKWebKit
的回答 var urlString = "www.apple.com"
if urlString.hasPrefix("https://") || urlString.hasPrefix("http://"){
let myURL = URL(string: urlString)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}else {
let correctedURL = "http://\(urlString)"
let myURL = URL(string: correctedURL)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
试试这个:
NSString *URL = @"apple.com" ;
NSURL *newURL ;
if ([URL hasPrefix:@"http://"] || [URL hasPrefix:@"https://"]) {
newURL = [NSURL URLWithString:URL] ;
}
else{
newURL = [NSURL URLWithString:[NSString
stringWithFormat:@"http://%@",URL]] ;
}
NSLog(@"New URL : %@",newURL) ;
我是这样解决的,Swift 4 及以上:
var yourString = "facebook.com"
var urlPath = ""
if yourString.contains(find: "https://") {
urlPath = path
} else {
let newPath = "https://\(path)"
urlPath = newPath
}
guard let url = URL(string: urlPath) else {return}
UIApplication.shared.open(url)