Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

ios - iOS7 Safari: Saving to Home-screen and persist token

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:)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...