ios - 如何将 html 文件加载到多个目标的 webView 中

标签 ios swift wkwebview nsbundle

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

两个目标均在目标成员(member)积分中选择。html 我尝试将身份检查器中文件的位置更改为所有可能的位置,但没有成功。 我猜想该文件以某种方式保存为只有主目标可以访问的路径。

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

最佳答案

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

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

关于ios - 如何将 html 文件加载到多个目标的 webView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54076489/

相关文章:

ios - uiscrollview 在 imageview 中改变图像

ios - NSXMLParser 将两个元素添加到一个 TableView 单元格

ios - iOS项目中的图片存放位置

ios - IOS中的ISP详细信息(运营商信息)

ios - 基于Notification有效负载快速显示Viewcontroller

objective-c - 下载 APNG 文件

ios - WKWebView 上的 AngularJS : any solution for handling file://on iOS 9?

json - 无法使用类型为 'String' 的索引为类型 [Dictionary<String, Any>] 的值下标

ios - iPhone 7 不显示网页 View

ios - 未识别的 CFRunLoopRun 崩溃报告问题