How to preload WKWebview from the WKWebview in swift5

I have two independent `WKWebViewController`. I load the first `WKWebView` and then another `WKWebView` when the button is pressed.



So I want to load another `WKWebView` in advance. How can I do this?



Each of the two ViewControllers has WKWebView.


https://ibb.co/8sRHCDN


First 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)
    }






Second 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
       
        secondWebWebView = WKWebView(frame: secondWebWebView.frame, configuration: config)
       
        secondWebWebView.uiDelegate = self
        secondWebWebView.navigationDelegate = self
       
        view.addSubview(secondWebWebView)
        view.addSubview(indicator)


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




How can I solve this problem?

Accepted Reply

I think I found a solution. But there's a problem. I can still see the animate image of the indicator. I don't think the search is complete.



First 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
        }
    }




Second WKWebView


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


So I solved the problem by concealing an indicator image when the screen was shown.



Second WKWebView


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




When you press the button to open the secondWebview screen, indicator images are displayed on the firstWebview screen, indicator images disappear when navigation is complete, and the secondWebview screen appears preloaded.

Replies

I think I found a solution. But there's a problem. I can still see the animate image of the indicator. I don't think the search is complete.



First 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
        }
    }




Second WKWebView


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


So I solved the problem by concealing an indicator image when the screen was shown.



Second WKWebView


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




When you press the button to open the secondWebview screen, indicator images are displayed on the firstWebview screen, indicator images disappear when navigation is complete, and the secondWebview screen appears preloaded.