iOS7 Safari: Saving to Home-screen and persist token
Asked Answered
A

1

25

For iOS 6.[something] onwards Cookies, SQLite data and localStorage data for Full Screen Web Apps is stored separately from the Safari data. I have a token that I need persisted to the Home-Screen app when saving to Home-Screen.

There is a cookie tester here. And a discussion here.

Has anyone found an elegant solution to this problem? Very ugly solutions will also be considered:)

Autocratic answered 14/1, 2014 at 9:10 Comment(0)
P
47

Since iOS 5 Apple have been making Home Screen bookmarks more and more isolated in terms of sharing data between the Safari browser and what is now considered a sandbox application.

  • iOS <= 5:Everything was cool, local storage, cookies, the works was shared between Home Screen and the web page. Transition between the site and Home Screen was seamless and web developers were happy.
  • iOS 6: Apple started treating Home Screen apps (including ones saved from the web) as sandboxed applications. Essentially the Home Screen app that displayed your saved site became a WebView control and was not embedding Safari itself. This prevented local storage from being transferred, but you could still share cookies.
  • iOS <= 6.x: Cookies became somewhat unreliable after a certain dot release, they were still shared, but did not get transferred on first save. If you went back to the site, and performed some action, the cookie would magically become available to the Home Screen app.
  • iOS 7: All forms of data sharing (local storage, cookies etc.) between Home Screen and the site from which it was saved have been lost (almost), crushing developer's dreams the world over.

At the time of writing (iOS 7.1 in beta), you have only one option.

  1. When your page loads, add your token/data to a query string parameter in the URL using JavaScript (window.location.search). When the user saves the site to their Home Screen, the URL is used as the key. Your JavaScript can easily extract the query string parameter you sneaked in earlier if you detect that you are in Home Screen mode (window.standalone). This should work forever, except the value in the URL will also be there forever.

  2. Another trick that is to 'render' any value you want to carry across to the Home Screen in the DOM. Since the early versions of iOS, the HTML that is rendered at the time it is saved to Home Screen, will remain unchanged. When launching a Home Screen web app, it does not request the content from the URL. It simply loads the HTML that was previously saved with no initial web request (all links to CSS and JS in the HTML will be requested, but not the page itself). With the knowledge that the HTML is forever saved, so will anything you rendered in it as well. The only way to update your page is to do a windows.reload or equivalent redirect if you aren't pulling your content via AJAX. Long story short, inject your value into a hidden input field for example and it will be transferred across.

Option #1 is will probably last forever, it's not likely the Home Screen will ever alter the URL in any way. Just make sure you have some JS in place to flip a switch and reload the page if you ever want to get rid of it.

Option #2 is somewhat risky because one day the all knowing Apple could decide that a initial page request should actually be made instead of using the same HTML at the point it was saved. This would then wipe out the data you have and the current state of any elements in the DOM.

UPDATE: The DOM injection technique is no longer valid either (iOS 7+), only the DOM directly downloaded from the server is saved to Home Screen. Any dynamic changes made during runtime are lost. Struck out option #2 since the question is for iOS 7 and it longer works, which leaves you with only option #1, adding something to the URL which will always get saved to Home Screen.

Pants answered 22/1, 2014 at 19:29 Comment(9)
Do you have any (official) sources for this? Maybe some official Apple docs, dev blogs or similar?Lodgment
@Lodgment Nope, this is purely from experience of doing this for the last couple years and having to adapt the solution for every release of iOS since v4.Pants
@karlingen Yes there is no reason that it wouldn't. When saving to Home Screen the URL is unmodified so it will always be available to your app. Apple would need to change something massively for this technique to fail. They should never alter the URL of what is being saved because that would affect the page being requested, so this should be good to go.Pants
@Pants is this still applicable for iOS 12?Appearance
@Appearance Not sure, haven't needed to use this technique for quite a few years now.Pants
@Pants this solution seems no longer to work, when saving to homescreen safari only saves the domain, no sub route, and no location search or parameters :(Whitethorn
@NickD Haven't needed to do any of these tricks for years now, guess someone else will need to figure out the best way with the current version of iOS and updated the answer.Pants
@Pants thanks for the response, your approach still work, the mistake was on my side, to get the PWA working on Android I added a PWA manifest.json file which declared the start_url as "/"Whitethorn
@NickD It seems to me that the incompatibility that you found between @BrutalDev's token/query string solution and the bare start_url in Android/Chrome's web app manifest is a fatal flaw in this technique, since it cancels out a prime advantage of a PWA, namely that it should work across browsers and OSs. I've posted a question that seeks a solution that works in iOS 14/Safari 14.Valentine

© 2022 - 2024 — McMap. All rights reserved.