1

I have a WebView in my Nativscript Vue app but on Android, the embedded website does not have access to localStorage within the browser. This question explains how to do it for Angular. The problem was that it was unclear where the webview instance was supposed to be gotten from. Question for completeness: How do I access the WebView instance that contains the android property in order to enable local storage on it?

4
  • You can of course add loaded listener and access webView.android in Vue. It's not a limitation for Vue. Commented Sep 23, 2019 at 16:27
  • 1
    Sorry @douira, may be you could try asking better or in a different way. The other SO thread you have linked already shows how exactly it could be enabled on Android. As I already mentioned Vue still supports all those events and native objects. So if you say you can't make it work with your Vue component, at least you should justify why. Commented Sep 24, 2019 at 17:25
  • I've clarified it now and answered with my solution. Please reconsider how bad this question was vs how it is now. Commented Sep 25, 2019 at 12:25
  • 1
    loadFinished event is triggered on every page navigation within WebView. Using loaded event saves it, you just have to do it once for WebView. Commented Sep 25, 2019 at 12:34

2 Answers 2

2

This is how I solved it in the end. I needed to access the WebView passed through the event handler in the property event.object. There is no event called onWebViewLoaded on the component since this is not Angular. Instead, we need to hook onto the loadFinished event on the WebView element.

<template>
  <Page>
    <WebView
      src="https://www.example.com/"
      @loaded="onLoaded"
    />
  </Page>
</template>

<script>
export default {
  methods: {
    //when the webview finishes loading
    onLoaded(event) {
      //get the webview
      const webView = event.object

      //if there is android
      if (webView.android) {
        //enable dom storage in the webview on android
        webView.android.getSettings().setDomStorageEnabled(true)
      }
    }
  }
}
</script>

Sign up to request clarification or add additional context in comments.

Comments

0

Just use Vuex combined with this plugin nativescript-localstorage. I say it's untested on Nativescript 6. I can say it works because I use it my self.

Example of the store.js

import Vue from 'vue';
import Vuex from 'vuex';
import localStorage from 'nativescript-localstorage';

const NSVuexPersistent = store => {
    let storageStr = localStorage.getItem('ns-vuex-persistent');
    if (storageStr) {
        store.replaceState(JSON.parse(storageStr))
    }
    store.subscribe((mutation, state) => {
        // Suscribe hook.
        localStorage.setItem('ns-vuex-persistent', JSON.stringify(state));
    })
};

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        nations: null
    },
    plugins: [NSVuexPersistent],
    mutations: {
        updateNations(state, nations) {
            state.nations= nations;
        }
    }
});

How does your main.js looks (add this):

import store from './store';

new Vue({
  store,
  render: h => h('frame', [h(App)])
}).$start()

2 Comments

this does not address the problem in any way. I'm asking about how to enable localStorage for a webView not how to persist nativescript state.
My fault. I misread your question. Like @Manoj said: So if you say you can't make it work with your Vue component, at least you should justify why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.