Android Webview & Localstorage

Android webview & localStorage

The following was missing:

settings.setDomStorageEnabled(true);

Access local storage within WebView

According to developer.android.com/reference/android/webkit/WebView.html:

For obvious security reasons, your application has its own cache,
cookie store etc.—it does not share the Browser application's data.
Cookies are managed on a separate thread, so operations like index
building don't block the UI thread. Follow the instructions in
CookieSyncManager if you want to use cookies in your application.

What I would suggest perhaps is to use deep linking. The flow would be something like:

  1. Save the activation code as before in the local storage
  2. When user downloads your app and goes thru the onboarding process
    include some button (or do it automatically) that opens up a web
    page in the system's browser.
  3. Now you have access to the local storage where you saved your
    activation code.
  4. On the website provide a deep linked button that has activation code
    in the url
  5. User clicks it and goes back to your app where you can grab
    activation code from the Intent

If you don't want to break UX by leaving the app perhaps you should take a look at referral links (search for: Google Play Campaign Measurement). You can access referral number within your app upon app downloading from Google Play Store. In that case you can omit accessing any local storage values and leaving your app.

How much does Android WebView localstorage can store?

You can check this post. It has a detailed explanation about the WebView Storage.

Access denied using local storage in Android WebView

Access to localStorage is only allowed on pages from certain "Web-safe" schemes, like http:, https: and file:. It doesn't work for about: and data: schemes for example, or for any custom scheme you might be using. Chrome behaves the same way, you can check on desktop.

Android - save value on LocalStorage before WebView.loadUrl()

What you can do is set a redirect into the javascript to the actual url and load the javascript plus redirect using webview.loadData().

 String injection = "<html><head><script type='javascript'>LocalStorage.set('namespace', 'key', 'value');window.location.replace('YOUR_URL_HERE');</script></head><body></body></html>";
webview.loadData(injection, "text/html", null);

Be aware though that you might need to save that to a file first before it works: see How to load a javascript redirect into android webview?

Android WebView not reading data from local storage in first time page finished?

I have a better solution for this it will work for the first time opening WebView.

you just have to use a custom callback. like this

public interface DataSavedCallback {
void onDataSaved();
}
  public static void getLocalStorageAndSaveOnAndroidPref(String[] keys,
WebView view,dataSavedCallback savedCallback) {

String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + keys[i] + "')"), new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: " + value);
}
});

}

then use this function to get the value of the key from the webview local storage

public static void getLocalStorageAndSaveOnAndroidPref(String[] keys,
WebView view,dataSavedCallback savedCallback) {

String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + keys[i] + "')"), new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: " + value);
}
});

}

you can call the above function anywhere using your webview

myWebView.post(() -> {
String[] keys = {"androidDeliveryAppStickyNotificationTitle","androidDeliveryAppStickyNotificationSubtitle","androidAppNewSoundAlertTitle"};
getLocalStorageAndSaveOnAndroidPref(keys, myWebView, new dataSavedCallback() {
@Override
public void onDataSaved() {
//do whatever you want
}
});

});



Related Topics



Leave a reply



Submit