为什么我不能立即看到预加载的WKWebView?

时间:2019-12-05 07:43:43

标签: javascript ios swift wkwebview

我正在使用html显示WKWebView文件。当您从主WKWebView打开另一个预加载的WKWebView时,有时在显示白屏之后,会发生此问题。当您看到屏幕时,它会闪烁。

Main.js

$(document).ready(function(){
var data = { 
            apiType : "preload",
            parameter : encodeURI(`view/other?value="testvalue"`)
}
webkit.messageHandlers.TestApp.postMessage(data);
...


$('#openBtn').on('click', function(){
   var data = { 
        apiType : "openOther"
       }
webkit.messageHandlers.TestApp.postMessage(data);

MainWKWebViewController.swift

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
            case "preload":
                let parameter = message["parameter"] as! String
                preload(parameter)
                break
            case "openOther":
                openOtherScreen()
                break
...

func preload(_ prameter: String){
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   otherWebView = storyboard.instantiateViewController(withIdentifier: "OtherWebViewController") as? OtherWebViewController
   otherWebView.delegate = self
   otherWebView.paramFromMain = prameter
   otherWebView.loadViewIfNeeded()
   otherWebView.otherWebView.navigationDelegate = self
   preloadCheck = true
}
func openOtherScreen(){
  if preloadCheck {
      DispatchQueue.main.async {
          self.navigationController?.pushViewController(self.otherWebView, animated: true)
          self.preloadCheck = false
     }
   } else {
   }
}

有人知道为什么有时会显示白色屏幕,闪烁并显示吗?还是有人解决了与我相同的问题?

EDIT:我还尝试了layoutIfNeeded()函数。但这是相同的。

func preload(_ prameter: String){
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   otherWebView = storyboard.instantiateViewController(withIdentifier: "OtherWebViewController") as? OtherWebViewController
   otherWebView.delegate = self
   otherWebView.paramFromMain = prameter
   otherWebView.view.layoutIfNeeded()
   DispatchQueue.main.async {
      self.otherWebView.loadViewIfNeeded()
      self.otherWebView.otherWebView.navigationDelegate = self
      self.preloadCheck = true
   }
}

我该如何解决这个问题?

0 个答案:

没有答案