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")
}
}
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.