0

I am showing html string on web view. But it takes 2-3 sec to show on the webview. When i am switching from one screen to another, then the web view content must be shown simultaneously. How can i do that ?

View Controller1

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func actionOpenWebView(_ sender: Any) {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
        self.navigationController?.pushViewController(vc, animated: true)
    }    
}

View Controller2

import UIKit
import WebKit

class ViewController2: UIViewController {

    @IBOutlet weak var webView: WKWebView!
    
    let str = "Coconut couscous with berries, poppy seeds and natural yogurt Coconut couscous with berries, poppy seeds and natural yogurt Coconut couscous with berries, poppy seeds and natural yogurt"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        webView.loadHTMLString(str, baseURL: nil)
        webView.uiDelegate = self
        webView.navigationDelegate = self
    }
    
    @IBAction func actionBack(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
}


extension ViewController2: WKUIDelegate, WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        print("Show Web View Content Successfully")   
    }
}
Rahul Chopra
  • 187
  • 1
  • 13

1 Answers1

0

Load the content to a webView and once this is done then present it to the user (see code below).

There are many ways of communicating between objects or communicating between views in UIKit. Anyway, the simplest way of communicating something between objects is a NotificationCenter.

import UIKit
import WebKit

class ViewController: UIViewController {
    
    var view2Controller: UIViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(forName: .some(.init("webViewDidFinishNavigation")), object: nil, queue: nil) { _ in
            self.webViewDidFinishNavigation()
        }
    }

    @IBAction func actionOpenWebView(_ sender: Any) {
        let view2Controller = storyboard!.instantiateViewController(withIdentifier: "View2Controller") as! View2Controller
        // load a controller view without presenting the controller, this will execute viewDidLoad method which loads data
        _ = view2Controller.view
        // store view2Controller for a moment when a webview will finish content loading
        self.view2Controller = view2Controller
    }
    
    func webViewDidFinishNavigation() {
        self.navigationController?.pushViewController(view2Controller!, animated: true)
    }
}

class View2Controller: UIViewController, WKNavigationDelegate {
    
    @IBOutlet var webView: WKWebView!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        self.webView.navigationDelegate = self
        DispatchQueue.global().async {
            sleep(5)
            DispatchQueue.main.async {
                self.webView.loadHTMLString("dezdz d ezdze dze", baseURL: nil)
            }
        }

    }
    
    @IBAction func actionBack() {
        self.navigationController?.popViewController(animated: true)
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        NotificationCenter.default.post(name: .init("webViewDidFinishNavigation"), object: nil)
    }
}
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93