简体   繁体   English

如何将本地HTML加载到UIWebView

[英]How to load local html into UIWebView

I am able to load up an external URL in a UIWebView like so: 我能够像这样在UIWebView中加载外部URL:

let url = NSURL(string: "http://www.google.com")
let request = NSURLRequest(URL: url)
webView.loadRequest(request)

but how do I load a local html file? 但是如何加载本地html文件?

The answer in this solution looked promising but Xcode says NSString.stringWithContentsOfFile() is deprecated. 解决方案的答案看起来很有希望,但Xcode表示已弃用NSString.stringWithContentsOfFile()。

You can do essentially what you're doing now, just use NSBundle to get a URL for your local file, like this: 您基本上可以做您现在正在做的事情,只需使用NSBundle来获取本地文件的URL,如下所示:

if let url = NSBundle.mainBundle().URLForResource("index", withExtension: "html") {
    webView.loadRequest(NSURLRequest(URL: url))
}

Thanks @Nate Cook. 感谢@Nate Cook。 Here is the update to Swift 3 这是Swift 3的更新

    if let url = Bundle.main.url(forResource: "about", withExtension: "html") {
        webView.loadRequest(NSURLRequest(url: url) as URLRequest)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM