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

查看:496
本文介绍了使用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,您应该使用 URLForResource NSBundle 类的方法。

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

Swift 2

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

Swift 3

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

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

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