PDF in WKWebview Disappears when Switching Tabs

I've run into an issue where a PDF that is correctly rendered disappears when switching to a different tab and then back to the original controller. The issue is easily reproducible using Xcode 10.1's Tabbed App template. I created a project using this template, and then placed the following code in SecondViewController (after removing the default subviews added by the template):


import UIKit
import WebKit

class SecondViewController: UIViewController {
    
    private lazy var webView: WKWebView = {
        let view = WKWebView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(webView)
        webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        let url = URL(string: "https://www.adobe.com/support/products/enterprise/knowledgecenter/media/c4611_sample_explain.pdf")!
        webView.load(URLRequest(url: url))
    }
}


When launching the app and switching to the second tab, the PDF renders correctly. Then, after switching back to the first tab and then back to the second it shows just a gray background with no PDF content. You can still scroll through the document and it will show page numbers, etc. But no content.


Any help or suggestions would be appreciated.

Show your reload code.

Hi
I am also facing the same issue did u get any solution.

This happens to me too. My current workaround is
Code Block
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        webView.reload()
    }

but this way the webview displays the pdf always from the top of the document and not where the user scrolled to which is very annoying.
I'm also experiencing this issue. I tried the workaround for reloading the webview in the viewWillAppear, but no joy for me (I'm loading the pdf from a local URL, so maybe that's the difference?).
I'm seeing this in iOS 13, but haven't tried the iOS 14 beta to see if it's resolved there.

What I ended up doing was load the PDF data myself (outside of the webKit view load method), caching the PDF data in memory, and then calling load like so:
Code Block          
webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: cachePath.deletingLastPathComponent())


But like replman's case, the contentOffset is reset to 0,0 when switching the tab back to the webkitview .
You could probably set delegate methods to cache the last known scroll offset, and then on the viewWillAppear, scroll to that last known offset. For my specific purpose, it's not worth it.
Same issue here. In my case I replaced the new WKWebView with the old UIWebView and everything worked fine again. Although deprecated the UIWebView seems less buggy.

This issue is still present in iOS 16. Did anyone ever find a better solution other than reloading?

I found that UIWebView crashes on iOS 16 when zooming the view, so I had to switch to WKWebView using the workaround described by @gregorosaurus. Additionally, I had to add a 0.4 second delay to the reload to make it work.

Meanwhile, since the issue still persists in iOS 17, and probably will never been fixed, the best solution is NOT to use WKWebView to display PDFs.

Using the PDFView from Apples PDFKit results in a better user experience.

https://developer.apple.com/documentation/pdfkit

PDF in WKWebview Disappears when Switching Tabs
 
 
Q