【问题标题】:How to load an html file into a webView for multiple targets如何将 html 文件加载到多个目标的 webView 中
【发布时间】:2019-01-07 14:41:14
【问题描述】:

在我的应用程序中,我有一个 html 信用文件,我想将它加载到 webView 中。我使用的代码非常适合我的主要目标。但是后来我创建了第二个目标:应用程序的专业版,但对于这个,代码不起作用,因为 url 为 nil。

在 Credits.html 的 Target Membership 中选择了两个目标 我尝试将身份检查器中文件的位置更改为所有可能性,但均未成功。 我猜该文件以某种方式保存在只有主要目标可以访问的路径中。

if let urlPath = Bundle.main.path(forResource: "credits", ofType: "html") {
    if let url = URL(string: urlPath) {
        webView.loadRequest(URLRequest(url: url))
    }
}

【问题讨论】:

    标签: ios swift wkwebview nsbundle


    【解决方案1】:

    您需要使用Bundle(identifier:) 来获取特定目标的捆绑包。您可以通过转到目标的构建设置并查看 Product Bundle Identifier 变量来了解每个特定目标的 Bundle 标识符。

    if let targetBundle = Bundle(identifier: "yourBundleId"), let filePath = targetBundle.url(forResource: "credits", withExtension: "html") {
        webView.loadRequest(URLRequest(url: url))
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多