iOS7 Safari:保存到主屏幕并保留令牌 [英] iOS7 Safari: Saving to Home-screen and persist token

查看:494
本文介绍了iOS7 Safari:保存到主屏幕并保留令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于iOS 6. [something]向上全屏Web应用程序的Cookie,SQLite数据和localStorage数据与Safari数据分开存储。我有一个令牌,我需要坚持到主屏幕应用程序,当保存到主屏幕。

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.

有一个Cookie测试员这里 。和讨论这里

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

推荐答案

自从iOS 5苹果已经使主屏幕书签越来越孤立在Safari浏览器和现在认为是沙箱应用程序之间共享数据。

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 :一切都很酷,本地存储,cookie,工作是在主屏幕和网页之间共享。

  • iOS 6 :苹果开始处理主屏幕应用程序(包括从网络中保存的应用程序)作为沙盒应用程序。基本上,显示您保存的网站的主屏幕应用程序成为WebView控件,并没有嵌入Safari本身。

  • iOS< = 6.x :某些点发布后,Cookie变得有点不可靠,他们仍然共享,但没有得到转移第一次保存。

  • iOS 7 :所有形式的数据都可以使用。

  • 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.

在撰写本文时(iOS 7.1测试版),您只有一个选项。

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


  1. 当您的网页加载时,使用JavaScript( window.location.search )将您的令牌/数据添加到网址中的查询字符串参数。当用户将站点保存到其主屏幕时,将使用URL作为键。如果您检测到您处于主屏模式( window.standalone ),您的JavaScript可以轻松地提取您之前潜入的查询字符串参数。

  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.

另一个技巧是渲染你想要的任何值以携带到DOM中的主屏幕。由于早期版本的iOS,在保存到主屏幕时呈现的HTML将保持不变。启动主屏幕网络应用程序时,不会请求网址中的内容。它只是加载以前保存的HTML,没有初始的Web请求(所有链接到CSS中的CSS和JS将被请求,但不是页面本身)。随着知识,HTML永远保存,所以你将呈现在其中的任何东西。更新网页的唯一方法是执行 windows.reload 或同等重定向,如果您不通过AJAX拉取您的内容。

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.

选项#2是有点儿的,只要确保你有一些JS翻转开关,并重新加载页面,如果你想要摆脱它。风险,因为有一天所有知道的苹果可以决定一个初始页面请求实际上应该,而不是使用相同的HTML在它保存的点。这会清除您所拥有的数据和DOM中任何元素的当前状态。

UPDATE :DOM注入技术不再有效(iOS 7+),只有从服务器直接下载的DOM保存到主屏幕。在运行时期间进行的任何动态更改都会丢失。删除选项#2,因为问题是为iOS 7,它更长的工作,这只有选项#1,添加的东西到URL将总是保存到主屏幕。

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.

这篇关于iOS7 Safari:保存到主屏幕并保留令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆