Wkwebview Is Showing Only Background

WKWebView shows a cropped background and blank space after rotation on iPad

This is a bug in WKWebKit that's usually triggered by HTML elements with position: fixed applied. The only workaround I've found is to use window.scrollTo() to scroll the page by one pixel a few hundred miliseconds after a resize event.

WKWebView shows pdf inside a border, I don't want the border

Solution :
I found a trick to do this, I think you can use this with pdf that has white background. It has the idea, you can manipulate this with your design.

self.view.backgroundColor = UIColor.white
let urlString = "http://www.africau.edu/images/default/sample.pdf"
let string = "<html><body marginwidth=\"0\" marginheight=\"0\" style=\"background-color: transparent\"><embed width=\"100%\" height=\"100%\" name=\"plugin\" src=\"\(urlString)\" type=\"application/pdf\"></body></html>"
webView.loadHTMLString(string, baseURL: nil)

Edit : If you want to use this with base64 encoded pdf data, you can use with that :

var pdfBase64String = "" //Your pdf data
if let decodedData = Data(base64Encoded: pdfBase64String, options: .ignoreUnknownCharacters) {
pdfWebView.load(decodedData, mimeType: "application/pdf", characterEncodingName: "utf-8", baseURL: URL(fileURLWithPath: ""))
}

Old Answer : Turning off "WebKitDiskImageCacheEnabled" can be fix this problem.
Can you try this before load the pdf ?

UserDefaults.standard.set(false, forKey: "WebKitDiskImageCacheEnabled")
UserDefaults.standard.synchronize()

Load WKWebView in background/off screen

You can add the WKWebView to the view hierarchy but its x is your current width, so it lays out of the screen but within the rendering hierarchy.

Add WKNavigationDelegate to your class and add it to the webView like

webView.navigationDelegate = self

Then implement the following function:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation)

This function and some others are called when the webview finished loading the content, BUT this does not include the 100% finished rendering. After that there is still some time required for rendering, the time consumption for this depends on the content that was loaded.

There currently is no callback for the 100% finished loading and rendering, so if you know the files, you can calculate or use a fix delay before moving the webView into the visible rect.

OR

If you feel fine with that, you observe private values in the webview and move your webview after those value changes to your preferred state. This looks for example like that:

class MyCustomWKWebView: WKWebView {

func setupWebView(link: String) {
let url = NSURL(string: link)
let request = NSURLRequest(url: url! as URL)
load(request as URLRequest)
addObserver(self, forKeyPath: "loading", options: .new, context: nil)
}

override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

guard let _ = object as? WKWebView else { return }
guard let keyPath = keyPath else { return }
guard let change = change else { return }

switch keyPath {
case "loading":
if let val = change[NSKeyValueChangeKey.newKey] as? Bool {
//do something!
}
default:
break
}
}

deinit {
removeObserver(self, forKeyPath: "loading")
}
}


Related Topics



Leave a reply



Submit