3

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?

Ben Saufley
  • 3,259
  • 5
  • 27
  • 42
  • If you haven't seen it already, [this answer](http://stackoverflow.com/a/12123291/1947535) may be relevant. Specifically the fact that Safari's localStorage writes to disk are asynchronous. – m.brindley Feb 06 '13 at 01:37
  • I actually had but I hadn't seen a comment beneath that unfortunately tells me all I need to know… – Ben Saufley Feb 06 '13 at 01:50

2 Answers2

3

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.

Community
  • 1
  • 1
Ben Saufley
  • 3,259
  • 5
  • 27
  • 42
0

I have not used the localStorage reference to a key with the dot notation. However localStorage.setItem("key", value) and localStorage.getItem("key") methods works.

Chong Yu
  • 470
  • 3
  • 8