Need content in UIWebView to display quickly(需要 UIWebView 中的内容快速显示)
问题描述
我的部分应用会缓存网页以供离线查看.为此,我保存从站点获取的 HTML 并重写 img url 以指向本地商店中的文件.当我将 html 加载到 UIWebView 中时,它会按预期加载图像,一切都很好.我也在以这种方式缓存样式表.
Part of my app caches web pages for offline viewing. To do that, I am saving the HTML fetched from a site and rewriting img urls to point to a file on the local store. When I load the html into a UIWebView, it loads the images as expected and everything's fine. I am also caching stylesheets in this fashion.
问题是当我将手机置于飞行模式时,加载这个缓存的 html 会导致 UIWebView 显示一个空白屏幕并在显示页面之前暂停一段时间.我发现这是由 Web 视图试图获取的原始 HTML 文档中引用的非缓存 URL 引起的.这些其他 URL 包括缓存样式表中的图像、iframe 中的内容以及打开连接以获取其他资源的 javascript.当 UIWebView 尝试获取这些资源时会发生暂停,并且网页仅在所有这些其他获取都超时后才会出现.
The problem is that when I put the phone into airplane mode, loading this cached html causes the UIWebView to display a blank screen and pause for a while before displaying the page. I've figured out that it's caused by non-cached URLs referenced from the original HTML doc that the web view is trying to fetch. These other URLs include images within the cached stylesheets, content in iframes, and javascript that opens a connection to fetch other resources. The pause happens when the UIWebView tries to fetch these resources, and the web page only appears after all these other fetches have timed out.
我的问题是,如何让 UIWebView 立即显示我缓存的内容?以下是我的想法:
My questions is, how can I make UIWebView just display the stuff I've cached immediately? Here are my thoughts:
- 编写更多代码来缓存这些其他引用.这可能需要大量代码来捕获所有边缘情况等,尤其是必须解析 Javascript 以查看页面加载后加载的内容
- 强制 UIWebView 立即超时,因此没有暂停.我还没想好怎么做.
- 即使外部引用尚未完成获取,也可以通过某种方式显示已加载的内容
- 剥离所有脚本、链接标签和 iframe 的代码以擦除"外部引用.我试过这个,但对于某些网站,结果页面严重混乱
有人可以帮我吗?我一直在做这件事,但我的想法已经不多了.
Can anyone help me here? I've been working on this forever, and am running out of ideas.
推荐答案
只是指出这个问题及其答案已有 4 年历史.我在下面聊iOS 3.x.我可以很容易地想象,与此同时情况发生了变化——哎呀,单是这些设备就比 iPhone 3G 更快.YMMV.:)
Just pointing out that this question, and its answers, are 4 years old. I'm chatting about iOS 3.x below. I can easily imagine that things have changed in the meantime -- heck, the devices alone are faster than the iPhone 3G was. YMMV. :)
我自己过去一段时间一直在研究这个,实际上还没有实现任何东西,只是在谷歌上搜索.总体而言,使用 DontLoad 在 iPhone 上执行此操作非常困难/不可能.即使在 3 月份,也有报道称它可以在模拟器中运行,但不能在设备上运行:http://discussions.apple.com/message.jspa?messageID=9245292
I've been looking at this myself for the past while, not actually implementing anything yet, just Googling. It seems overall that it's very hard / impossible to do this on the iPhone using DontLoad. Even in March, there were reports that it works in Simulator, but not on device: http://discussions.apple.com/message.jspa?messageID=9245292
我只是猜测,但基于其他 StackOverflow 问题(例如 这个) 我在想,设备上的缓存可能比它的 Mac 和 Safari 对应的缓存更有限.
I'm just guessing, but based on even other StackOverflow questions (e.g. this one) I'm thinking perhaps the on-device caching is somehow more limited than both its Mac and Safari counterparts.
Apple 论坛上的其他人在询问 UIWebView 和 NSURLCache 时报告了粗鲁工程师的经历,在错误报告中工程师说它应该可以工作,但实际开发人员说它没有.http://discussions.apple.com/thread.jspa?threadID=1588010(7月29日-8月20日)
Others on the Apple forums report experiences with rude engineers when asking about UIWebView and NSURLCache, where in bug reports the engineers say it should work, but actual developers say it doesn't. http://discussions.apple.com/thread.jspa?threadID=1588010 (Jul 29-Aug 20)
一些解决方案可能在这里.考虑到 [UIImage imageWithData:...],David 描述了一些用于 iPhone 的异步缓存图像下载器的实现"的代码.
Some solutions might be found here. Considering [UIImage imageWithData:...], David describes some code for "an implementation of an asynchronous, caching image downloader for iPhone."
现在我在 cocoa-dev 邮件列表中发现了这封 8 月 10 日的电子邮件:
And now I've discovered this Aug 10th email on the cocoa-dev mailing list:
2009 年 8 月 10 日下午 1:38,Mike Manzano 写道:
On Aug 10, 2009, at 1:38 PM, Mike Manzano wrote:
有没有人能够成功获取 iPhone 上的 URL 加载系统注意自定义版本NSURL缓存?在我看来,它是被忽略了.更多信息在这里:
Has anyone been able to successfully get the URL loading system on iPhone to pay attention to custom versions of NSURLCache? It seems to me that it's being ignored. More info here:
[此页面的链接;已删除.]
[a link to this page; removed.]
(请参阅第二个答案"页).
(See the second "answer" on that page).
如果我需要一些线索做错事或者我的代码只是被忽视.
Just need some sort of clue if I'm doing something wrong or if my code's just being ignored.
是的,我已经成功了 - 它只是需要制作自己的缓存实例并明确设置共享缓存到该新实例.
Yes, I've had it work - it just requires making your own cache instance and explicitly setting the shared cache to that new instance.
但它确实有效 - 一次实施,我的设置表格视图细胞图像到网络图像"效果很好(并且没有滚动充其量是痛苦的).
But it absolutely did work - once implemented, my "set the table view cell's image to an image from the net" works nicely (and without it scrolling was painful at best).
格伦·安德烈亚斯·甘德里亚斯@xxxxxxxxxxxxhttp://www.gandreas.com/ 恶趣味!疯狂、坏和危险的知识
Glenn Andreas gandreas@xxxxxxxxxxxx http://www.gandreas.com/ wicked fun! Mad, Bad, and Dangerous to Know
上面的邮件可以在http://找到www.cocoabuilder.com/archive/message/cocoa/2009/8/10/242484(没有进一步的回复)
The above email can be found at http://www.cocoabuilder.com/archive/message/cocoa/2009/8/10/242484 (with no further replies)
在 iCab 博客:iPhone 上 UIWebView 的 URL 过滤(2009 年 8 月 18 日)
Further proof that NSURLCache can work is found in iCab Blog: URL filtering for UIWebView on the iPhone (Aug 18, 2009)
这里的任何人可能还应该查看 Apple 的 URLCache 示例应用程序,因为它可能与他们的工作相关:https://developer.apple.com/iphone/library/samplecode/URLCache/index.html 除了现在才看,它说这个应用程序不使用 NSURLCache磁盘或内存缓存,因此我们的缓存策略是通过从源加载数据来满足请求."并使用 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
.所以这没有我想象的那么重要,为什么 这个人说 NSURLCache 是个笑话.
Anyone here probably should also look to Apple's URLCache sample app as it may be relevant to their work: https://developer.apple.com/iphone/library/samplecode/URLCache/index.html Except having just looked at it now, it says "This application does not use a NSURLCache disk or memory cache, so our cache policy is to satisfy the request by loading the data from its source." and uses cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
. So this is less relevant than I thought, and why this person was calling NSURLCache a joke.
所以看起来 NSURLCache 并不像我开始这篇文章所说的那样棘手或不可能.一定要喜欢找到一种行之有效的方法,同时研究它行不通的原因.我仍然不敢相信iphone"和NSURLRequestReturnCacheDataDontLoad"只有 25 个 Google 搜索结果......这个答案几乎每一个都有.而且我很高兴我写了它,以便我以后可以参考它;-)
So it seems NSURLCache is not as tricky or impossible as I started this post by saying. Gotta love finding a way that works while researching reasons it won't work. I still can't believe there are only 25 Google search results for "iphone" and "NSURLRequestReturnCacheDataDontLoad"... This answer has just about every one of em. And I'm glad I wrote it so I can refer to it later ;-)
这篇关于需要 UIWebView 中的内容快速显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:需要 UIWebView 中的内容快速显示
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- URL编码Swift iOS 2022-01-01
- UITextView 内容插图 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01