John Doe
John Doe

Reputation: 1043

iOS WKWebView does not support local storage

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.

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.

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)
    }
}

My WKWebView is apparently missing some feature or setting that allows it to store offline data.

Upvotes: 11

Views: 22434

Answers (1)

amarjit singh
amarjit singh

Reputation: 149

You need to add this

webConfiguration.websiteDataStore = WKWebsiteDataStore.default()

Apple documentation doesn't clearly state it but I think the default WKWebsiteDataStore is persistent.

Read here about WKWebsiteDataStore/ Local Storage

Further, you can also check whether the data store is persistent or not.

Upvotes: 14

Related Questions