【问题标题】:Add http:// to NSURL if it's not there如果不存在,请将 http:// 添加到 NSURL
【发布时间】:2015-10-31 06:24:45
【问题描述】:

我在我的应用程序中使用 Web 视图,从文本字段中获取 URL。如果字符串以“http://”开头,它就可以工作。我正在尝试修改代码,以便它也可以处理用户不输入“http://”或“https://”的情况

如何检查网址中是否没有“http://”? 怎么修改网址添加“http://”?

NSString *URLString = textField.text;
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];

【问题讨论】:

标签: ios objective-c cocoa-touch nsurl


【解决方案1】:
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];

【讨论】:

  • = [NSURL new]; 没有理由。这是浪费内存。
【解决方案2】:

让我更新对 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)
    }

【讨论】:

    【解决方案3】:

    试试这个:

        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) ;
    

    【讨论】:

      【解决方案4】:

      试试这个。

      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 hasPrefix:]
      • 是的,因为如果 http:// 位于字符串中的其他位置而不是前面,则不会加载。 :)
      【解决方案5】:

      我这样解决了,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)
      

      【讨论】:

        猜你喜欢
        • 2011-12-16
        • 1970-01-01
        • 2011-02-15
        • 2011-09-04
        • 2012-05-25
        • 2019-07-20
        • 1970-01-01
        • 2014-03-06
        • 1970-01-01
        相关资源
        最近更新 更多