Swift打开带参数的本地HTML文件

时间:2016-05-26 16:36:55

标签: html swift uiwebview

我有一个本地HTML文件,我想在ProbabilityPair中加载,但也想在里面传递一个参数。我可以加载HTML文件

UIWebView

我想将本地传递给HTNL,如webView.loadRequest(NSURLRequest(URL: NSBundle.mainBundle().URLForResource("apprules", withExtension: "html")!)) 。有没有办法在Swift中做到这一点?

3 个答案:

答案 0 :(得分:7)

Swift 3及以上:

let url1 = Bundle.main.url(forResource: "apprules", withExtension: "html")!
let url2 = URL(string: "?lang=ja", relativeTo: url1)!
webView.loadRequest(NSURLRequest(url: url2))

Swift 2及以下:

let url1 = NSBundle.mainBundle().URLForResource("apprules", withExtension: "html")!
let url2 = NSURL(string: "?lang=ja", relativeToURL: url1)!
webView.loadRequest(NSURLRequest(URL: url2))

答案 1 :(得分:0)

对于Swift 3,我修改了代码。多谢你们。这篇文章对我有很大帮助。

    let url1 = Bundle.main.url(forResource: "apprules", withExtension: "html")!
    let url2 = NSURL(string: "?lang=ja", relativeTo: url1)!
    webView.loadRequest(NSURLRequest(url: url2 as URL) as URLRequest)

答案 2 :(得分:0)

快速5:

let url1 = Bundle.main.url(forResource: "apprules", withExtension: "html")!
let url2 = URL(string: "?lang=ja", relativeTo: url1)!
let myRequest = URLRequest(url: url2) 
WKWebView.load(myRequest)