使用 swift 将本地 html 加载到 UIWebView [英] Load local html into UIWebView using swift

查看:18
本文介绍了使用 swift 将本地 html 加载到 UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天一大早就让我发疯了.我想将一些本地 html 加载到 web 视图中:

This one is driving me crazy early this morning. I want to load some local html into a web view:

class PrivacyController: UIViewController {

    @IBOutlet weak var webView:UIWebView!

    override func viewDidLoad() {
        let url = NSURL(fileURLWithPath: "privacy.html")
        let request = NSURLRequest(URL: url!)
        webView.loadRequest(request)
    }
}

html 文件位于我项目的根文件夹中,但在一个组内.webview对我来说是空白的.有什么想法有什么问题吗?我在 xcode 6.1 上并在我的 iphone 6 上运行这个示例.

The html file is located in the root folder of my project but is inside a group. The webview is blank for me. Any ideas whats wrong? I am on xcode 6.1 and running this example on my iphone 6.

推荐答案

要获取应用程序资源的 URL,你应该使用 NSBundle 类的 URLForResource 方法.

To retrieve URLs for application resources, you should use URLForResource method of NSBundle class.

斯威夫特 2

let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html") 

斯威夫特 3

let url = Bundle.main.url(forResource: "privacy", withExtension: "html")

这篇关于使用 swift 将本地 html 加载到 UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆