需要UIWebView中的内容才能快速显示 [英] Need content in UIWebView to display quickly

查看:106
本文介绍了需要UIWebView中的内容才能快速显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的部分应用会缓存网页以供离线观看。为此,我保存从站点获取的HTML并重写img urls以指向本地存储上的文件。当我将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以擦除外部引用。我试过这个,但是对于一些网站来说,结果页面严重混乱

  • write even more code to cache these other references. This is potentially a ton more code to catch all the edge cases, etc., especially having to parse the Javascript to see what it loads after the page is loaded
  • force UIWebView to time out immediately so there's no pause. I haven't figured out how to do this.
  • somehow get what's already loaded to display even though the external references haven't finished fetching yet
  • strip the code of all scripts, link tags and iframes to "erase" the external references. I've tried this one, but for some sites, the resultant page is severely messed up

有人可以帮我吗?我一直在努力解决这个问题,而且我的想法已经不多了。

Can anyone help me here? I've been working on this forever, and am running out of ideas.

推荐答案

编辑:只是指出这个问题,及其答案,已有4年历史。我正在聊聊下面的iOS 3.x.我可以很容易地想象事情在此期间发生了变化 - 哎呀,单独的设备比iPhone 3G更快。因人而异。 :)

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月份,也有报告说它可以在Simulator中运行,但不能在设备上运行: 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问题(例如< a href =https://stackoverflow.com/questions/345432/is-it-possible-to-cache-resources-loaded-in-an-iphone-uiwebview>这一个)我在想也许设备上的缓存在某种程度上比它的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时会报告粗鲁工程师的经历,在bug报告中,工程师说它应该工作,但实际的开发人员说它没有。 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加载系统以关注
NSURLCache的自定义版本?在我看来,这是
被忽略了。更多信息:

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需要某种线索,如果我是
做错了什么,或者我的代码的
被忽略了。

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

Glenn Andreas gandreas @ xxxxxxxxxxxx
http://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)

进一步证明NSURLCache可以工作的证据可以在 iCab Blog: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磁盘或内存缓存,所以我们的cach e政策是通过从其来源加载数据来满足请求。并使用 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中的内容才能快速显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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