在我的应用程序中,我有一个 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/