I'm in the process of making a mobile app for a client, using Buzztouch, so the html pages are not within the app, they are loaded in from the server via JSON.
The client would like to have a screen on the app where users can enter details into a timetable and store it on the phone.
I'm trying to do this using localStorage, and have setup all the permissions, etc but I get the following error when trying to read or write to the localstorage on the page:
"SecurityError: Failed to read the 'localStorage; property from 'Window': Access is denied for this document."
This is on an Asus Memopad running Android version 5.
Here are the permissions in the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
Here are the webView settings:
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
webView.getSettings().setDomStorageEnabled(true);
I've tried the exact same code on an older Android tablet, running Android version 2.3, and I don't get this error, localStorage works fine.
I've also tried in on an iPhone and it works fine.
I even tried copying the code onto a standard webpage and it works fine in the browser on the tablet, but webView still doesn't like it.
I've looked around on the net for hours, and so far I haven't found a solution to this.
Any ideas?
UPDATE:
Here is some of the jQuery code, it is meant to check if localstorage is available, and load in an item if it is set.
try {
if(typeof(Storage) !== "undefined") {
try {
if(localStorage.getItem("storage") !== null) {
storage = JSON.parse(localStorage.getItem("storage"));
}
} catch(e) {
$("#error").html($("#error").html() + " " + e + "<br>");
$("#error").css("display", "block");
}
} else {
$("#log").html("HTML5 Storage is not working.");
}
} catch(e) {
$("#error").html($("#error").html() + " " + e + "<br>");
$("#error").css("display", "block");
}
This is what is throwing the error on load, as this is in the $(document).ready section.
I've finally managed to solve this problem.
The only way I could get it to work, no matter what I tried, was to include the html file within the app, rather than doing it through Buzztouch's control panel, and loading that in.
This html file now loads correctly and with no localStorage errors.
This error is actually not resulting from lacking any Java-side permissions, it's due to improved web security model of newer WebView versions. It's hard to tell what the app is doing wrong without seeing the actual code, but my guess is that you are probably trying to access localStorage from a page of a different origin. E.g. if you are using
loadData
for your initial page, and then load other parts from a server, that code can have different origin.