繁体   English   中英

预加载多个本地 WebView

[英]Preload multiple local WebViews

因为我只能找到一些过时的信息/无法解决我的问题,所以我决定再次提出此类问题。 (参见过时/错误的解决方案:)


我的应用程序分为一个本机部分和一个 HTML 部分。 HTML 保存为本地文件 (index.html),应该加载到myWebView视图中。

@IBOutlet weak var myWebView: UIWebView!
func loadWebview() {
    let url = Bundle.main.url(forResource: "index", withExtension: "html")
    let request = URLRequest(url: url!)
    myWebView.loadRequest(request)
    myWebView.scrollView.isScrollEnabled = false
    myWebView.allowsLinkPreview = false
    myWebView.delegate = self
}

因为我的 DOM 树非常大,所以从本机部分切换到Web 部分(点击按钮时)至少需要很长时间 -至少第一次切换- 因为之后,我确定webView-request被缓存。

To my question :从本机切换到 Web 部件时,如何在app init预加载 WebView以避免出现白屏(可能是 0.5s - 1s 持续时间)?

编辑:

WKWebView 正在显示滚动条,而 UIWebView 没有!

使用(与 UIWebView 一样)这种样式:

::-webkit-scrollbar {
     display: none;
}

不工作并添加这些行:

webview.scrollView.showsHorizontalScrollIndicator = false
webview.scrollView.showsVerticalScrollIndicator = false

也根本不工作。

首先,你应该切换到WKWebViewUIWebView不再被苹果推荐使用。

其次,您可以创建一个 Web 视图池,这些视图会在应用程序启动时被创建并要求加载。 这样,当用户切换到 Web 界面时,Web 视图可能有机会完全加载。

为此,您可以使用这样的类:

/// Keeps a cache of webviews and starts loading them the first time they are queried
class WebViewPreloader {
    var webviews = [URL: WKWebView]()


    /// Registers a web view for preloading. If an webview for that URL already
    /// exists, the web view reloads the request
    ///
    /// - Parameter url: the URL to preload
    func preload(url: URL) {
        webview(for: url).load(URLRequest(url: url))
    }

    /// Creates or returns an already cached webview for the given URL.
    /// If the webview doesn't exist, it gets created and asked to load the URL
    ///
    /// - Parameter url: the URL to prefecth
    /// - Returns: a new or existing web view
    func webview(for url: URL) -> WKWebView {
        if let cachedWebView = webviews[url] { return cachedWebView }

        let webview = WKWebView(frame: .zero)
        webview.load(URLRequest(url: url))
        webviews[url] = webview
        return webview
    }
}

并要求它有时在应用程序启动期间预加载 url:

// extension added for convenience, as we'll use the index url in at least
// two places
extension Bundle {
    var indexURL: URL { return self.url(forResource: "index", withExtension: "html")! }
}

webviewPreloader.preload(url: Bundle.main.indexURL)

第三,您可能需要在控制器中使用容器视图而不是实际的 Web 视图:

@IBOutlet weak var webviewContainer: UIView!

剩下的就是在需要时将预加载的 web 视图添加到容器中:

func loadWebview() {
    // retrieve the preloaded web view, add it to the container
    let webview = webviewPreloader.webview(for: Bundle.main.indexURL)
    webview.frame = webviewContainer.bounds
    webview.translatesAutoresizingMaskIntoConstraints = true
    webview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    webviewContainer.addSubview(webview)
}

最后一点,请注意保持 Web 视图的活动实例可能会带来性能损失 - 内存和 CPU 方面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM