gpt4 book ai didi

ios - 如何在swift5中从WKWebviews预加载WKWebviews

转载 作者:行者123 更新时间:2023-12-01 19:38:36 30 4
gpt4 key购买 nike

我有两个独立的WKWebViewController。当按下按钮时,我先加载WKWebView,然后加载另一个WKWebView

所以我想提前加载另一个WKWebView。我怎样才能做到这一点?

两个ViewController均具有WKWebView。

mainstory

第一个WKWebView

    func openSecondScreen(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let openSecondScreen = storyboard.instantiateViewController(withIdentifier: "SecondWebViewController") as! SecondWebViewController
openSecondScreen.modalPresentationStyle = .overCurrentContext
openSecondScreen.modalTransitionStyle = .crossDissolve
openSecondScreen.delegate = self
self.navigationController?.pushViewController(openSecondScreen, animated: true)
}

第二个WKWebView

    @IBOutlet var secondWKWebView: WKWebView!
override func loadView() {
super.loadView()

let images = global.getIndicatorImage()
let animatedImage = UIImage.animatedImage(with: images, duration: 1.0)
self.indicatorImage.image = animatedImage


contentController.add(self, name: "SecondWeb")

config.userContentController = contentController

secondWKWebView = WKWebView(frame: secondWKWebView.frame, configuration: config)

secondWKWebView.uiDelegate = self
secondWKWebView.navigationDelegate = self

view.addSubview(secondWKWebView)
view.addSubview(indicator)

if contentController.userScripts.count > 0 {
contentController.removeAllUserScripts()
}
let localFilePath = Bundle.main.url(forResource: webUrl, withExtension: "html")
let myRequest = URLRequest(url: localFilePath)
secondWKWebView.load(myRequest)
}

我怎么解决这个问题?

最佳答案

我想我找到了解决方案。但是有一个问题。我仍然可以看到指示器的动画图像。我认为搜索不完整。

第一个WKWebView

    var openSecondScreen : SecondWebViewController!
var preloadCheck = false
...
func openSecondScreen(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
openSecondScreen = storyboard.instantiateViewController(withIdentifier: "SecondWebViewController") as! SecondWebViewController
openSecondScreen.delegate = self
openSecondScreen.loadViewIfNeeded()
openSecondScreen.secondWKWebView.uiDelegate = self
openSecondScreen.secondWKWebView.navigationDelegate = self
preloadCheck = true
}
....
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if preloadCheck {
self.navigationController?.pushViewController(openSecondScreen, animated: true)
preloadCheck = false
}
}

第二个WKWebView

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
indicatorImage.isHidden = true
indicatorImage.stopAnimating()
}

因此,我通过在显示屏幕时隐藏指示器图像来解决此问题。

第二个WKWebView

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
indicatorImage.isHidden = true
indicatorImage.stopAnimating()
}

当您按下按钮以打开 secondWebview屏幕时, firstWebview屏幕上显示 指示器图像,导航完成后 指示器图像消失,并且 secondWebview屏幕出现 preloaded

关于ios - 如何在swift5中从WKWebviews预加载WKWebviews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58796829/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com