Search code examples
javascriptioshtmllocal-storageoffline-caching

LocalStorage not passing to standalone web app


I just need to hold onto some simple data in an iOS standalone web app (using apple-mobile-web-app-capable etc). The data is defined when the user visits the site, and saved to localStorage.name etc. That same page is "saved to Home Screen" but when I get there, outputting localStorage.name etc returns undefined.

I go back to Safari and it still knows the information. I pull it up with my laptop's Developer Console and I can still get all the data. It's not even a different page, let alone a different domain or anything. Everything I read tells me that localStorage should (with minor caveats for modifying that data) be shared between Safari and the standalone app. What am I missing?

Here's the gist of the code:

if (!window.navigator.standalone) {
    // show Safari content
    if (localStorage.name != void 0 && localStorage.var2 != void 0) {
        // show normal, "add this to your home screen" language -
        // this screen shows properly
    } else {
        // show "We weren't passed our variables" language
    }
} else {
    // Show standalone/offline content
    if (localStorage.name != void 0 && localStorage.var2 != void 0) {
        // Show normal standalone app content using localStorage variables.
        // I'm not seeing this.
    } else {
        // Show "error" text, which is what I'm getting
        document.body.removeChild(document.getElementById('container'));
        var helper = document.createElement('section')
            helper.id="helper"
        helper.innerHTML = '<h1>Sorry</h1>'
          +   '<p>There seems to be a problem with the app.</p>'
          +   '<pre>'
          +   "  Name: " + localStorage.name + "\n"
          +   "  var2: " + localStorage.var21 + "\n"
          +   '</pre>';
        document.body.insertBefore(helper,document.body.firstChild)
    }
}

For what it's worth, I'm in iOS 6.1. So it shouldn't be an "old" problem. What am I doing wrong?


Solution

  • This comment: iOS 'Web App' has different localStorage than Mobile Safari

    …has the answer. The local storage is now entirely separate from Safari to the native app.