今天一大早这个让我发疯。我想将一些本地 html 加载到 WebView 中:
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 上运行这个例子。
最佳答案
要检索应用程序资源的 URL,您应该使用 NSBundle
类的 URLForResource
方法。
swift 2
let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html")
swift 3
let url = Bundle.main.url(forResource: "privacy", withExtension: "html")
关于ios - 使用 swift 将本地 html 加载到 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26647447/