WKWebview iOS - 首次启动速度缓慢

问题描述 投票:0回答:1

我正在使用 WKWebview 显示一个简单的 html 页面。当我第一次单击按钮以使用 WKWebview 导航到该控制器时,它会挂起几秒钟,然后打开。这种情况仅在应用程序启动后第一次发生。

控制台消息:

[Process] WebContent process (0x127060f00) took 3.816507 seconds to launch

我使用此代码导航到 webview 控制器。

let webviewController = self.storyboard?.instantiateViewController(withIdentifier: "webview") as! WebviewController;
self.navigationController?.pushViewController(webviewController, animated: true)

如何避免这种延迟?

ios swift xcode wkwebview
1个回答
0
投票

我使用与这个答案相同的方法。似乎在任何地方做一次就足够了:

let webView = WKWebView()
webView.loadHTMLString("", baseURL: nil)

这会消除第一次启动的延迟,当您启动实际视图时,速度会更快。我已将其实现为在应用程序启动后启动:

struct MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
...
}


class AppDelegate: NSObject, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        let webView = WKWebView()
        webView.loadHTMLString("", baseURL: nil)
        return true
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.