【问题标题】:How to open local html file on Safari?如何在 Safari 上打开本地 html 文件?
【发布时间】:2017-01-03 17:15:11
【问题描述】:

我想在我的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 集成中?

提前致谢!

【问题讨论】:

  • 能否请您分享您的代码以在 safari 中显示本地文件(您当前分享的代码 sn-p 不是)。还要粘贴控制台错误输出?

标签: html ios swift safari swift3


【解决方案1】:

你不能使用SFSafariViewController

来自 Apple 文档:

1.选择最佳 Web 查看类

如果您的应用允许用户从 Internet 上的任何位置查看网站,请使用 SFSafariViewController 类。如果您的应用自定义、交互或控制 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>

ViewContrller.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)
    }
}

输出:

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多