Load ViewController's WebView before showing

Mills

New Member
#1
I have an app with a ViewController that contains a WebView. Because the website the WebView is showing is quite large, I'd like to load the ViewController while the app is launching so as soon as the user opens the ViewController with the WebView it will show the already loaded page.
I've tried overriding viewDidLoad(), loadView(), a convenience init() etc. but nothing I have put there seems to get executed before actually showing the ViewController.
Mã:
 let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
 webViewVC = storyboard.instantiateViewController(withIdentifier: "WebViewController")

 if let c = webViewVC.childViewControllers[0] as? WebViewController {

     c.dDelegate = self

     webViewVC.modalPresentationStyle = .popover
     let popover = webViewVC.popoverPresentationController!
     popover.delegate = self
     popover.permittedArrowDirections = .up
     webViewVC.loadView()
     present(webViewVC, animated: true, completion: nil) //if I don't add this line, the ViewController doesn't seem to get created before showing
 }
So what can I call before present() in order to execute some of the ViewController's code? I need to load stuff like the following:
Mã:
let wwConfiguration = WKWebViewConfiguration()

webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.layer.frame.width, height: self.view.layer.frame.height), configuration: wwConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
let url = URL(string: . . .
 

Admin

Administrator
Thành viên BQT
#2
It's not entirely clear from your question, but I'm assuming that you're presenting your web view controller from another view controller from, let's say, a button press. Something like this…

EIg0e.png

What you need to do is to handle this asynchronously.

  • Instantiate your web view controller
  • Start loading its web view
  • Wait for that to complete
  • Present the web view controller
Example…

WebViewController

Mã:
class WebViewController: UIViewController {

    @IBOutlet private weak var webView: WKWebView!

    var didLoadWebView: (() -> Void)!

    func loadWebView(completion: @escaping () -> Void) {
        loadViewIfNeeded()
        didLoadWebView = completion // Save this closure to be called when loaded
        webView.navigationDelegate = self
        webView.load(URLRequest(url: URL(string: "http://apple.com")!))
    }
}

extension WebViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        didLoadWebView() // Call back to the presenting view controller
    }
}
Presenting ViewController
Mã:
class ViewController: UIViewController {

    @IBAction func showWebView(_ sender: UIButton) {

        let wvc = storyboard!.instantiateViewController(withIdentifier: "WebViewController") as! WebViewController
        wvc.modalPresentationStyle = .popover
        wvc.popoverPresentationController?.sourceView = sender
        wvc.popoverPresentationController?.sourceRect = sender.bounds
        wvc.loadWebView {
            self.present(wvc, animated: true, completion: nil)
        }
    }
}
 
OP
OP
M

Mills

New Member
#3
I really have no idea how I couldn't think of putting it into a non-native function this morning. Thanks a lot for this, sorry for wasting your time hahaha I feel stupid now
 
Top