ios - WKWebView reloads on each segue, instead of maintaining it's last state
问题描述
I currently have two ViewControllers:
- ViewController: The welcome/dashboard view.
- WebViewController: The view that gets passed URLs to load in a WKWebView.
Here is my situation: I have a number of objects in my ViewController (mostly UIButtons and UIBarButtonItems), each with it's own unique URL, that will then segue (Show/Push) that object's specific URL to the WebViewController and load said URL in the WKWebView.
However, when loading the same URL (which I have implemented a check for), the WKWebView will still reload the page each time that same URL is requested. When pushed to WebViewController with the same URL that was loaded prior, the WKWebView will:
- Display the page it had loaded prior for half a second
- Flash white while it attempts to reload
- And then reloads that exact same URL
I simply want the WebViewController's WKWebView to maintain its already-loaded state if the requested URLs are the same.
ViewController.swift
@IBAction func goDashboard(_ sender: UIButton) {
Page.query = Page.dashboard
performSegue(withIdentifier: "WebViewSegue", sender: nil)
}
@IBAction func goNewProject(_ sender: UIButton) {
Page.query = Page.newProject
performSegue(withIdentifier: "WebViewSegue", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "WebViewSegue" {
let webVC = segue.destination as! WebViewController
webVC.query = Page.query
}
}
WebViewController.swift
var query = URL(string: Client.url) // Default value
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
if debug { print("Loading URL: \(String(describing: query))") }
webView.uiDelegate = self
webView.navigationDelegate = self
webView.customUserAgent = Client.userAgent // Set Client UserAgent
// WebView Configuration
let config = webView.configuration
config.applicationNameForUserAgent = Client.userAgent // Set Client Name
config.preferences.javaScriptEnabled = true // Enable JavaScript
// ScrollView Setup
webView.scrollView.delegate = self
webView.scrollView.isScrollEnabled = true // Enable Scroll
webView.scrollView.keyboardDismissMode = .onDrag // Hide Keyboard on WebView Drag
//showScroll(false, bounce: true) // Hide Scroll, Enable Bounce
webView.scrollView.alwaysBounceHorizontal = false
webView.scrollView.showsHorizontalScrollIndicator = false
if !Page.isSame() {
print("Queries are different, load new page")
webView.load((query ?? Page.dashboard)!)
} else {
print("Queries are the same, DON'T RELOAD!")
}
}
EDIT: I have also tried stopping the data from passing between the ViewController's if the URLs match.
if segue.identifier == "WebViewSegue" {
if !Page.isSame() {
let webVC = segue.destination as! WebViewController
webVC.query = Page.query
}
}
EDIT 2: I have also attempted to get rid of the prepare segue.identifier
and, instead, added the check to WebViewController using global variables. It still reloads each time.
解决方案
创建一个类型的全局可选变量,WebViewController
并在每个 segue 触发器上检查它是否包含值。
如果它不包含值,则创建一个实例并将其分配给全局变量并执行 segue。
如果它包含意味着您
WebViewController
已经创建和加载的值,只需使用全局实例。
另一种解决方案是使用cache data
. 而不是每次都加载页面,而是通过cache data
.
推荐阅读
- amazon-web-services - 通过 CloudFormation 授予 Lake Formation 对 Glue 数据库中所有表的读取权限
- firebase - Firestore 模拟器和 doc.metadata
- nginx - 当我在代理服务器中使用 expires 时,代理忽略标头不起作用
- multithreading - 将结构作为 P 线程参数传递 Linux 无法转换为指针类型
- javascript - 使用按钮更改 textarea 值的问题(vanilla JS)
- python - 重复KFold & cross_val_predict
- r - 最后的观察结果向前添加
- python - 如何合并多个数据框并将它们显示在 python 的一个箱线图中?
- java - 在 Android 中以编程方式连接到 Wifi AP
- r - 检查日期是否在R中的间隔内非常慢