如何在iOS中的WebView中打开URL?

时间:2016-10-12 06:41:46

标签: ios url uiwebview nsurl

我试图在WebView中打开以下网址

http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com

但无法制作NSURL的对象。

4 个答案:

答案 0 :(得分:2)

你的代码在哪里? 你使用了webView吗?

我使用了这种方法:

[_webView loadRequest:[NSURLRequest requestWithURL:url]];

答案 1 :(得分:1)

<强>步骤1

  

最初在.plist中添加NSAppTransportSecurity,请参阅this

<强>步骤-2

  

调用网址

NSString *urlAddress =  @"http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com"; 
[self.yourwebviewName loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]]];

答案 2 :(得分:1)

尝试使用URL代替addingPercentEncodingstringByAddingPercentEscapesUsingEncoding进行编码,因为它已被弃用。

在Swift中:

  • 适用于Swift 3

    let strUrl = "http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com"
    let encodedUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    
  • 对于Swift 2.3或更低版本

     let encodedUrl = strUrl.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
    

在Objective-C

NSString *strUrl = @"http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com";
NSString *encodedUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

答案 3 :(得分:0)

swift

$('#word').load()

objective-c

var url : NSString = "http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com" 
var urlStr : NSString = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 
var searchURL : NSURL = NSURL(string: urlStr)! 
    webviewInstance.loadRequest(NSURLRequest(URL: searchURL))