Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 447
John Doe Avatar asked Feb 05 '18 11:02

John Doe


People also ask

What is the difference between UIWebview and WKWebView?

Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

What is IOS WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

Is WKWebView secure?

The WKWebView is a modern API applying all the modern web security mechanisms, it's still maintained by Apple and gets updates. The good thing about WKWebView is that it does out-of-process rendering, so if the attackers find a memory corruption vulnerability in it, your application's process is still isolated.


1 Answers

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.

like image 195
amarjit singh Avatar answered Oct 23 '22 17:10

amarjit singh