我在 iOS 中遇到了 HTML5 离线应用程序的问题。我的应用程序在 Firefox、Chrome 和 Android 2.2 中离线运行良好,但在运行 iOS 4.2.1 的 iPod Touch 上无法正常运行。
这是我的清单(一个 JSP),称为“1.cache.manifest.jsp”。我使用“no-cache.jsp”JSP 来询问清单是否未缓存。我还将“index.jsp”添加到清单中,尽管这不是必需的,因为它是引用清单的资源。
<%@page contentType="text/cache-manifest; charset=UTF-8" pageEncoding="UTF-8"
%><jsp:include page="no-cache.jsp" flush="true"
/><%
String cacheManifestVersion = "20110220 1224";
//System.out.println("Cache manifest version=" + cacheManifestVersion);
%>CACHE MANIFEST
index.jsp
cache-this.js.jsp
这是我的 index.jsp 页面。它侦听 applicationCache 事件并转储事件类型。我使用“no-cache.jsp”JSP 来询问不缓存 HTML。
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%><jsp:include page="no-cache.jsp" flush="true"
/><!DOCTYPE html>
<html manifest="1.cache.manifest.jsp">
<head>
<script>
var appCacheEvents = ["checking", "error", "noupdate", "downloading", "progress", "updateready", "cached", "obsolete"];
for (var i = 0; i < appCacheEvents.length; i++) {
applicationCache.addEventListener(appCacheEvents[i], function (evt) {
var el = document.getElementById("applicationCache-events");
el.innerHTML += "applicationCache " + evt.type + " event.<br/>";
}, false);
}
</script>
<script src="./cache-this.js.jsp"></script>
</head>
<body>
<div id="applicationCache-events"></div>
<div id="cache-this-output"></div>
</body>
</html>
“cache-this.js.jsp”是一些在加载时向页面添加一些文本的 javascript:
<%@page contentType="application/javascript; charset=UTF-8" pageEncoding="UTF-8"
%><jsp:include page="no-cache.jsp" flush="true"
/>// cache this
window.addEventListener("load", function (evt) {
var msg = "Script loaded " + new Date();
document.getElementById("cache-this-output").innerHTML = msg;
}, false);
这是那些工作的用户代理的输出,第一次访问该站点时:
applicationCache checking event.
applicationCache downloading event.
applicationCache progress event.
applicationCache progress event.
applicationCache cached event.
Script loaded Sun Feb 20 2011 13:22:33 GMT+0000 (GMT Standard Time)
随后的输出是:
applicationCache checking event.
applicationCache noupdate event.
Script loaded Sun Feb 20 2011 13:23:47 GMT+0000 (GMT Standard Time)
当离线时(在 Firefox 中),我得到以下信息。请注意“错误”事件,但该应用程序确实可以脱机工作(即使在我清除 HTTP 缓存之后)。
applicationCache checking event.
applicationCache error event.
Script loaded Sun Feb 20 2011 13:26:54 GMT+0000 (GMT Standard Time)
在我的 iPod Touch 上,除了“缓存”事件被“错误”事件替换之外,我得到了相同的输出(作为第一次访问)。
任何想法为什么iOS最初无法缓存应用程序?