gpt4 book ai didi

html - 如何在 Safari 上打开本地 html 文件?

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:25 26 4
gpt4 key购买 nike

我想在我的 Swift 3 应用程序上打开一个关于 Safari 集成的本地 html 文件。

我知道如何使用 URL 来做到这一点。这是我用来执行此操作的代码:

let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

let svc = SFSafariViewController(url: NSURL(string: encodedString!) as! URL)
self.present(svc, animated: true, completion: nil)

但我无法对本地文件执行相同的操作。我已将我的 html 文件复制到我的项目中,我可以在目录树上看到它,但我无法使其工作。我看过Load local html into UIWebView using swift供引用。

如何将本地 html 文件加载到 Safari 集成中?

提前致谢!

最佳答案

您不能使用 SFSafariViewController 来做到这一点

From Apple Documentation:

1.选择最佳的 Web 查看类

如果您的应用允许用户从互联网上的任何地方查看网站,请使用 SFSafariViewController类(class)。如果您的应用自定义、交互或控制 Web 内容的显示,请使用 WKWebView类。

2。如果您查看 init 的声明

convenience init(url URL: URL)

url: 要导航到的 URL。 URL 必须使用 httphttps 方案。

使用 Webkit 或 WebView

helloAshok.html

<html>
<head>
</head>
<body>
<br />
<h2> Welcome ASHOK</h2>
</body>
</html>

ViewController.swift

class ViewController: UIViewController {
@IBOutlet weak var myWebView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

let localFilePath = Bundle.main.url(forResource: "helloAshok", withExtension: "html")
let request = URLRequest(url: localFilePath!)
myWebView.loadRequest(request)
}
}

输出:

enter image description here

关于html - 如何在 Safari 上打开本地 html 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41448858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com