简体   繁体   English

永久重定向iOS主屏幕Web应用

[英]Permanently Redirect iOS Home Screen Web Apps

I have a single-page web app using Sencha Touch that has been added to the home screen on about 2000 iPads. 我有一个使用Sencha Touch的单页Web应用程序,该应用程序已添加到大约2000个iPad的主屏幕上。 I am looking to change the URL of the web app without requiring all of those users to delete the launch icon from the home screen, go to the new URL, and add it to home screen again. 我希望更改Web应用程序的URL,而不要求所有这些用户都从主屏幕删除启动图标,转到新的URL,然后再次将其添加到主屏幕。 The app also uses a cache manifest to cache the HTML, CSS, JavaScript, and images, and the app is capable of working entirely offline. 该应用程序还使用缓存清单来缓存HTML,CSS,JavaScript和图像,并且该应用程序能够完全脱机工作。

Web pages added to home screen in iOS do seem to respond to HTTP 301 (permanent redirect) as expected, but I have found the behavior to be quirky in iOS 8 on iPad 2 devices as I will describe. 在iOS中添加到主屏幕的网页似乎确实按预期响应了HTTP 301(永久重定向),但是正如我将要描述的那样,我发现在iPad 2设备上的iOS 8中这种行为是古怪的。 I created an ASP.NET MVC website that I deploy under the same URL, replacing the Sencha Touch app in order to accomplish the permanent redirect. 我创建了一个ASP.NET MVC网站,该网站以相同的URL部署,替换了Sencha Touch应用程序以完成永久重定向。 Here is the process I'm using and the behaviors I'm seeing: 这是我正在使用的过程以及我看到的行为:

  1. When the app is launched from the old URL, it requests only the cache.manifest, and I return HTTP 404 to make the app stop caching. 从旧的URL启动应用程序时,它仅请求cache.manifest,我返回HTTP 404以使应用程序停止缓存。

  2. The app loads and I have an event handler in the app's JavaScript for the applicationCache "obsolete" event that will call window.location.reload(true). 应用程序加载,并且我在应用程序的JavaScript中有一个事件处理程序,用于applicationCache“过时”事件,该事件将调用window.location.reload(true)。 The app will then reload and this time will ask for the previously cached HTML page (which is hosted at the URL root), and my ASP.NET MVC site will then return HTTP 301 to permanently redirect to the new URL. 然后,该应用程序将重新加载,这次将请求以前缓存的HTML页面(位于URL根目录),然后我的ASP.NET MVC站点将返回HTTP 301以永久重定向到新URL。

  3. Once the app hits the new URL, it begins downloading the resources from the cache.manifest at the new URL. 应用点击新网址后,它将开始从cache.manifest中的新网址下载资源。 I have an event handler on the "updateready" applicationCache event that will call window.location.reload(true) and reload the app. 我在“ updateready” applicationCache事件上有一个事件处理程序,该事件处理程序将调用window.location.reload(true)并重新加载应用程序。 Once the app reloads, it will then request all resources (XHR requests to services) from the new URL as expected. 重新加载应用程序后,它将按预期从新URL请求所有资源(对服务的XHR请求)。

  4. When I test this on an iPad 1 running iOS 5, this works exactly as I would expect. 当我在运行iOS 5的iPad 1上进行测试时,它的工作与我期望的完全一样。 Once the resources from the new URL are downloaded and cached, it always makes every request from the new URL from that point. 一旦下载并缓存了来自新URL的资源,它将始终从该新URL发出每个请求。 I can put the device in airplane mode and the app will work just fine offline. 我可以将设备置于飞行模式,并且该应用可以在离线状态下正常运行。

  5. This is where the quirky behavior begins, and I only see this on my iPads that are running iOS 8.x (I don't have any devices running iOS 6 or 7). 这是古怪行为开始的地方,我只在运行iOS 8.x的iPad上看到此情况(我没有任何运行iOS 6或7的设备)。 I then close the app by pressing the home button and relaunch it from the home screen icon. 然后,我通过按下主屏幕按钮关闭应用程序,然后从主屏幕图标重新启动它。 When I relaunch the app, it will always initially go back to the old URL (iOS 5 always goes to the new URL), which is strange because the HTTP 301 from before should have prevented this. 当我重新启动该应用程序时,它最初总是会返回到旧的URL(iOS 5始终会返回到新的URL),这很奇怪,因为以前的HTTP 301应该阻止了此操作。 From here, there are two possible behaviors: 从这里开始,有两种可能的行为:

5a. 5a。 Sometimes, it will ask for only the HTML page from the old URL (root URL), and in that case, it will get another HTTP 301 and will then redirect to the new URL, ask for the cache manifest, then load the app. 有时,它将仅从旧URL(根URL)中请求HTML页面,在这种情况下,它将获得另一个HTTP 301,然后将重定向到新URL,询问缓存清单,然后加载应用程序。 From there and onward, it will never make requests to the old URL again when I open and close the app. 从那里开始,当我打开和关闭应用程序时,它将永远不会再次向旧URL发出请求。 When I put the device in airplane mode, the app works just fine offline. 当我将设备置于飞行模式时,该应用可以在离线状态下正常运行。 I have two iPad 2s running iOS 8.3 and 8.4, and this will happen about 50% of the time on these devices. 我有两个运行iOS 8.3和8.4的iPad 2,在这些设备上大约有50%的时间会发生这种情况。

5b. 5b。 Other times, it doesn't work so well. 其他时候,它不能很好地工作。 When relaunching the app after downloading the cached resources from the new URL, it will go back to the old URL and will not request the HTML page, and will instead request the cache.manifest along with the CSS and JavaScript. 从新的URL下载缓存的资源后重新启动应用程序时,它将返回到旧的URL,并且不请求HTML页面,而是请求cache.manifest以及CSS和JavaScript。 The the cache.manifest request will be result in another HTTP 404, but if I continue closing and re-opening the app, it will never consider the cache to be obsolete and will not request the HTML page again. cache.manifest请求将导致另一个HTTP 404,但是如果我继续关闭并重新打开该应用程序,它将永远不会认为缓存已过时并且不会再次请求HTML页面。 Interestingly, I am only able to reproduce this on the iPad 2s. 有趣的是,我只能在iPad 2s上重现这一点。 I have an iPad Air 1 running iOS 8.3 and I only ever see 5a on that device. 我有一台运行iOS 8.3的iPad Air 1,但在该设备上只能看到5a。

  1. For the case described in 5b, it does request the JavaScript files. 对于5b中描述的情况,它确实请求JavaScript文件。 So, I went into one the JavaScript files and put in window.location.reload(true), which causes it to request the HTML page, which results in another HTTP 301. Now when this scenario occurs, it does send it to the new URL, but every time I close and re-open the app, it repeats the whole cycle. 因此,我进入一个JavaScript文件,并放入window.location.reload(true),这导致它请求HTML页面,从而导致另一个HTTP301。现在,在发生这种情况时,它会将其发送到新的URL,但是每次我关闭并重新打开该应用程序时,都会重复整个过程。 It goes to the old URL, gets the JavaScript, reloads, gets a 301, then goes to the new URL. 它转到旧URL,获取JavaScript,重新加载,获取301,然后转到新URL。 When I put the device in airplane mode and open the app, it doesn't work. 当我将设备置于飞行模式并打开应用程序时,它不起作用。

I found that if I put the iPad 2s in airplane mode and run the app in the old URL, then run it again and do the 404 for the cache.manifest and the 301 for the HTML, then 5a will always occur. 我发现,如果我将iPad 2s置于飞行模式并以旧网址运行该应用程序,然后再次运行并为cache.manifest执行404,为HTML执行301,则5a总是会发生。 This sounds like a bug in iOS 8 (that possibly also exists in 6 and 7), and I'm trying to figure out a workaround that I can implement in my ASP.NET MVC website to redirect to the new URL that will work 100% of the time. 这听起来像是iOS 8中的错误(可能也存在于6和7中),我试图找出一种解决方法,可以在我的ASP.NET MVC网站中实施以重定向到可以使用100的新URL。 % 的时间。

Any insights would be greatly appreciated. 任何见解将不胜感激。

I figured out how to avoid the quirky behavior. 我想出了如何避免古怪的行为。 As I mentioned above, the app is listening to the "obsolete" event on applicationCache and is then calling window.location.reload(true) to grab the page again and get the 301. I found that if instead of reloading the same page, I redirect to a different page and back again, it works perfectly 100% of the time. 如上所述,该应用程序正在侦听applicationCache上的“过时”事件,然后调用window.location.reload(true)再次获取该页面并获取301。我发现,如果不重新加载同一页面,我重定向到另一个页面,然后再次返回,它在100%的时间内都能正常运行。 It redirects to the new URL, caches the resources, then never asks for the old URL again. 它重定向到新的URL,缓存资源,然后再也不需要再次请求旧的URL。 I also discovered that closing the app and opening it again after the cache is obsoleted has the same effect. 我还发现,在缓存被废弃后关闭应用程序并再次打开它具有相同的效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于IOS的Polymer Web应用程序主屏幕 - Polymer Web apps Home screen for IOS 调试在iOS Safari上添加到主屏幕的Web应用程序 - Debugging web apps added to home screen on iOS Safari IOS以编程方式将应用程序移动到主屏幕的文件夹中 - IOS programmatically move apps into a folder at home screen 通过 iOS 或 Android 上的“添加到主屏幕”添加的 Web 应用程序的命名约定? - Naming convention for Web Apps added via the Add to Home Screen on iOS or Android? iOS:Apple 是否为保存到主屏幕的 web 应用禁用了 HTML5 离线功能? - iOS: Did Apple disable HTML5 offline capability for web apps saved to the home screen? 如何为iOS Web应用程序创建“添加到主屏幕”说明页面 - How do I create an “Add to Home Screen” instruction page for iOS web apps iOS设备上安装的所有应用程序列表及其在主屏幕(Springboard)上的位置 - List of all apps installed on iOS device and their position on the Home Screen (Springboard) 如何:主屏幕应用程序和ios 6上的离线使用 - 缓存清单已过时? - How To: Home Screen Apps and offline usage on ios 6 – cache manifest obsolete? iOS4清除缓存错误?! 带有“添加到主屏幕”应用程序和localStorage - IOS4 Clear cache bug?! with “Add to Home Screen” apps and localStorage 如何在iOS上以编程方式创建“主屏幕应用程序文件夹”按钮? - How to create 'home screen apps folder'-like button programmatically on iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM