UIWebView not showing content when peeking on a UIButton using 3D Touch

318 views Asked by At

I am looking to achieve the effect where you have a link to a website and can preview it using 3D Touch, like in Apple's Notes app for example.

In my storyboard I have a UIButton (which opens a link) in one of my UIViewControllers and I have another UIViewController of the custom type WebViewController (storyboard id: WebView), containing only a UIWebView. I have implemented 3D Touch on the UIButton and I can display anything I want in the peek view, yet my UIWebView doesn't show its content when I display it in the peek view. I have used the UIWebViewDelegate functions to confirm that the UIWebView is loading the url successfully. The only problem is that it doesn't show anything in the UIWebView, I just get a blank UIWebView when I use 3D touch on the UIButton.

This is the code for the 3D Touch peek implementation in my UIViewController:

func previewingContext(_: UIViewControllerPreviewing, viewControllerForLocation: CGPoint) -> UIViewController? {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let previewController = storyboard.instantiateViewController(withIdentifier: "WebView") as? WebViewController
    previewController?.loadView()
    previewController?.webView.loadRequest(URLRequest(url: URL(string: "https://google.com/")!))
    return previewController
}

I have used the following UIWebViewDelegate methods to check whether the UIWebView is loading the url:

func webViewDidStartLoad(_ webView: UIWebView) {
    print("Started loading")
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    print("Finished loading")
}

func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
    print("WebView error: \(error)")
}

Those give me the following output, so I assume the url is loading fine:

Started loading
Finished loading

How do I get the UIWebView to display the loaded url in the peek view?

1

There are 1 answers

0
Adriaan On BEST ANSWER

I got the problem solved by contacting Hector from KrakenDev (all credits to him), who suggested I moved the loading code for the UIWebView to the WebViewController's viewWillAppear(). It's all working now.