我想打开本地 html 文件Safari
集成到我的Swift 3
应用。
我知道如何使用网址来做到这一点。这是我用来执行此操作的代码:
let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let svc = SFSafariViewController(url: NSURL(string: encodedString!) as! URL)
self.present(svc, animated: true, completion: nil)
但我无法对本地文件执行相同的操作。我已将我的 html 文件复制到我的项目中,我可以在目录树上看到它,但我无法使其工作。我看过使用 swift 将本地 html 加载到 UIWebView 中 https://stackoverflow.com/questions/26647447/load-local-html-into-uiwebview-using-swift以供参考。
如何将本地 html 文件加载到 Safari 集成中?
提前致谢!
你不能使用SFSafariViewController https://developer.apple.com/reference/safariservices/sfsafariviewcontroller
来自苹果文档:
1. 选择最佳的网页浏览类别
如果您的应用程序允许用户从 Internet 上的任何位置查看网站,请使用SFSafariViewController https://developer.apple.com/reference/safariservices/sfsafariviewcontroller班级。如果您的应用程序自定义、交互或控制 Web 内容的显示,请使用WKWebView https://developer.apple.com/reference/webkit/wkwebview class.
2.如果你看一下声明init https://developer.apple.com/reference/safariservices/sfsafariviewcontroller/1621222-init
convenience init(url URL: URL)
url:
要导航到的 URL。该 URL 必须使用http
or https
scheme.
使用 Webkit 或 WebView
你好阿肖克.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)
}
}
Output:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)