Observer
Observer

Reputation: 377

Passing & Retriving localStorage value in Android Webview

Background:

  1. In android Webview:

    • Html file saved Locally.
  2. When user is Online

    • URL is called and Page A is loaded from Website.
    • certain data is stored in localStorage('X') on Page A.
  3. When user is offline

    • Html Page B (saved locally) is displayed.
    • Page B fetch data from localStorage('X') and store in localStorage('Y').
    • Page B perform operations on localStorage('Y') in JavaScript.
  4. When user is Online again

    • Page A fetch data from localStorage('Y') and store in localStorage('X').

Issue

localStorage('X') value is not passed to localStorage('Y') & vice-versa.

Looking for:

Ways to pass localStorage Value from Website to local HTML file & Vice-Versa

Upvotes: 0

Views: 615

Answers (1)

Nisarg Shah
Nisarg Shah

Reputation: 14561

You cannot share data across two domains using localStorage.

One of the methods used to share data across domains is through use of postMessage API. In addition to the MDN link, you can find some more details here: How do you use window.postMessage across domains?

In your case, you may need to make the following changes:

  • When user is Online

    • Locally saved Page C is opened
    • Page C opens Page A in an iframe element.
    • Whenever Page A needs to save any data to localStorage, it sends the data via postMessage to the parent page, i.e. Page C. In turn Page C can save the data to localStorage.
  • When user is offline

    • Html Page B (saved locally) is displayed.
    • It can refer to the data in localStorage stored by Page C, because both were opened offline.

Upvotes: 1

Related Questions