ios - 开发iOS框架时如何在WKWebView中加载本地HTML文件

标签 ios swift cocoapods wkwebview

我开发了一个 iOS 框架,用户可以将其与 CocoaPod 一起使用。但是在项目中使用的时候,出现了这一行错误:

let url = Bundle(for: type(of: self)).url(forResource: "file_name", withExtension: "html", subdirectory: "assets")!

该行在框架中,错误是:

Unexpectedly found nil while unwrapping an Optional value

在框架中,我有一个名为“assets”的文件夹和一个名为“file_name.html”的文件。仅当在框架中使用时才会出现该错误。

最佳答案

let url = Bundle(for: type(of: self)).url(forResource: "file_name", withExtension: "html", subdirectory: "assets")!
-------------------------------^

确保 self 指向框架的一个类,以便找到正确的包。并且您不需要使用 type(of:_):

let bundle = Bundle(for: FrameworkClass.self)
let url = bundle.url(forResource: "file_name", withExtension: "html", subdirectory: "assets")!

关于ios - 开发iOS框架时如何在WKWebView中加载本地HTML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55517523/

相关文章:

ios - 为什么在 MacBook 上查看时我的背景图像会变形?

objective-c - 在dealloc中应该将什么样的属性设置为nil?

ios - 在开发期间将文件复制到 NSDocumentDirectory 或从 NSDocumentDirectory 复制文件

objective-c - UIButton CustomButton图像未出现

ios - 添加本地动态框架后 Cocoapods Mach-O 链接器错误

ios - 从嵌入的 tableviewcontroller 中执行Segue时如何保留NavigationController

ios - 无法在 iOS 11、Swift 4 的特定 View Controller 中隐藏状态栏

swift - 在 MapKit 中添加注释 - 以编程方式

macos - 如何查看已安装的 cocoa pod 的版本?

parse-platform - CocoaPods 存储库上的 "PFFacebookUtils.framework"在哪里?