【发布时间】:2022-12-07 12:55:29
【问题描述】:
我添加了一个 WebView 来加载 HTML 和 baseUrl 方法。但这会导致应用程序崩溃并显示警告
不应在主线程上调用此方法,因为它可能会导致 用户界面无响应。
//load HTML let htmlPath = Bundle.main.path(forResource: "index", ofType: "html") let folderPath = Bundle.main.bundlePath let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true) do { let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue) self.webView.loadHTMLString(htmlString as String, baseURL: URL(string: newBaseURL)) } catch { // catch error print(error.localizedDescription) }我在 viewDidLoad() 中调用了这段代码。它还已添加到调度队列中。任何帮助深表感谢。
【问题讨论】:
-
我希望 self.webView 是 WKWebView 也是你在 XCode 日志中遇到的错误是什么? index.html 文件是否存在于您的包中?
-
@Satheesh 它显示并加载 iframe 但是当加载大数据时它会导致崩溃
-
崩溃日志,好吗?
-
@Satheesh 现在它没有显示崩溃,但会导致 ui 无响应并断开 iphone。甚至无法调试..
-
为什么不使用您创建的
bundleURL变量?代码中使用但未显示的newBaseURL变量是什么?为什么要从folderPath创建bundleURL?为什么不直接使用Bundle.main.bundleURL?
标签: ios swift xcode webview wkwebview