iOS WKWebView不支持本地存储 [英] iOS WKWebView does not support local storage
问题描述
我正在构建一个简单的WKWebView应用程序,该应用程序可以加载用Construct(HTML5)编写的游戏.
I am building a simple WKWebView app that loads a game written in Construct (HTML5).
游戏存储在服务器上,当我在常规浏览器(移动和台式机)上玩游戏时,游戏本身存储在本地,高分也存储在本地.重新启动后,该游戏不需要重新下载,我可以看到以前的高分.
The game is stored on a server, when I play the game in a regular browser (both mobile and desktop) the game itself gets stored locally and also high-scores are stored locally. After relaunch the game does not require a re-download as well as I can see the previous high-score.
我不知道Construct用于存储本地数据的确切方式,但是当我在WKWebView中运行完全相同的游戏时,该游戏也不会存储在本地,也不会获得高分.
I dont know what exactly does Construct use to store local data, but when I run the exact same game in my WKWebView the game does not get stored locally nor do the high-scores.
我的Swift代码是这样的:
My Swift code is this:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let htmlPath = Bundle.main.path(forResource: "game", ofType:"html")
let url = URL(fileURLWithPath: htmlPath!)
let request = URLRequest(url: url)
webView.load(request)
}
}
我的WKWebView显然缺少允许其存储脱机数据的某些功能或设置.
My WKWebView is apparently missing some feature or setting that allows it to store offline data.
推荐答案
您需要添加此
webConfiguration.websiteDataStore = WKWebsiteDataStore.default()
Apple文档没有明确说明,但我认为默认的WKWebsiteDataStore是持久的.
Apple documentation doesn't clearly state it but I think the default WKWebsiteDataStore is persistent.
在此处阅读有关WKWebsiteDataStore/本地存储的信息
此外,您还可以检查数据存储是否持久是否.
Further, you can also check whether the data store is persistent or not.
这篇关于iOS WKWebView不支持本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!