html - 在 macOS 应用程序中使用 Swift 3 在 WebView 中加载本地 HTML 文件

标签 html swift xcode macos webview

我在这方面遇到了困难。我不太确定我做错了什么。

这是我为 iOS UIWebView 做的:

    let url = Bundle.main.url(forResource: "index", withExtension: "html")
    let request = NSURLRequest(url: url!)
    webView.loadRequest(request as URLRequest)

不幸的是,这不适用于 OS X 应用程序。我找到了一些用于 Swift 2 的代码。我对其进行了一些更改以使其与 Swift 3 一起使用,但我仍然遇到错误。

    let url = Bundle.main.url(forResource: "index", withExtension: "html")
    self.webView.mainFrame.load(NSURLRequest(URL: NSURL(string: url)!))

我有哪些选择?提前致谢!

最佳答案

试试这个简单的步骤,对我有用

let urlpath = Bundle.main.path(forResource: "index", ofType: "html");
let requesturl = URL(string: urlpath!)
let request = URLRequest(url: requesturl!)
webView.mainFrame.load(request)

关于html - 在 macOS 应用程序中使用 Swift 3 在 WebView 中加载本地 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41378026/

相关文章:

ios - Swift 中的 UITableView – 未知异常

ios - 解析推送通知添加新操作

ios - 从 Swift 2 迁移的项目的 Swift 包管理器

ios - SwiftUI 列表未更新内拉以刷新 View

javascript - 从 javascript 捕获 window.open

html - 创建顶级菜单项和右登录菜单

php - CSS 样式在 PHP 中不起作用

javascript - 使用 onclick 验证绘制图像 Hotpot

ios - 在 View 内创建导航 Controller

ios - UILabel 移动到点而不捕捉