如何从 swift5 中的 WKWebviews 预加载 WKWebviews

How to preload WKWebviews from the WKWebviews in swift5

我有两个独立的WKWebViewController。当按下按钮时,我加载第一个 WKWebView,然后加载另一个 WKWebView

所以我想提前加载另一个WKWebView。我该怎么做?

两个 ViewController 中的每一个都有 WKWebView。

第一个 WKWebView

    func openSecondScreen(){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let openSecondScreen = storyboard.instantiateViewController(withIdentifier: "SecondWebViewController") as! SecondWebViewController
        openSecondScreen.modalPresentationStyle = .overCurrentContext
        openSecondScreen.modalTransitionStyle = .crossDissolve
        openSecondScreen.delegate = self
        self.navigationController?.pushViewController(openSecondScreen, animated: true)
    }

第二个 WKWebView

    @IBOutlet var secondWKWebView: WKWebView!
    override func loadView() {
        super.loadView()

        let images = global.getIndicatorImage()
        let animatedImage = UIImage.animatedImage(with: images, duration: 1.0)
        self.indicatorImage.image = animatedImage


        contentController.add(self, name: "SecondWeb")

        config.userContentController = contentController

        secondWKWebView = WKWebView(frame: secondWKWebView.frame, configuration: config)

        secondWKWebView.uiDelegate = self
        secondWKWebView.navigationDelegate = self

        view.addSubview(secondWKWebView)
        view.addSubview(indicator)

        if contentController.userScripts.count > 0 {
            contentController.removeAllUserScripts()
        }
        let  localFilePath = Bundle.main.url(forResource: webUrl, withExtension: "html")
        let  myRequest = URLRequest(url: localFilePath)
        secondWKWebView.load(myRequest)
    }

我该如何解决这个问题?

我想我找到了解决办法。但是有一个问题。我仍然可以看到指标的动画图像。我认为搜索没有完成。

第一个 WKWebView

    var openSecondScreen : SecondWebViewController!
    var preloadCheck = false
   ...
    func openSecondScreen(){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        openSecondScreen = storyboard.instantiateViewController(withIdentifier: "SecondWebViewController") as! SecondWebViewController
        openSecondScreen.delegate = self
        openSecondScreen.loadViewIfNeeded()
        openSecondScreen.secondWKWebView.uiDelegate = self
        openSecondScreen.secondWKWebView.navigationDelegate = self
        preloadCheck = true
    }
....
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        if preloadCheck {
            self.navigationController?.pushViewController(openSecondScreen, animated: true)
            preloadCheck = false
        }
    }

第二个 WKWebView

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        indicatorImage.isHidden = true
        indicatorImage.stopAnimating()
    }

所以我通过在显示屏幕时隐藏指示图像来解决问题。

第二个 WKWebView

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        indicatorImage.isHidden = true
        indicatorImage.stopAnimating()
}

当您按下按钮打开secondWebview屏幕时,指示图像显示在firstWebview屏幕上,指示图像 导航完成后消失,secondWebview 屏幕出现 preloaded