【问题标题】:Caching web page in IOS using NSURLCache subclass使用 NSURLCache 子类在 IOS 中缓存网页
【发布时间】:2013-05-23 09:56:05
【问题描述】:

我继承了 NSURLCache 并覆盖了 – cachedResponseForRequest:– storeCachedResponse:forRequest: 。我将获取的所有文件都存储在应用程序的文档目录中。

现在,问题是,.html 文件在打开时不会加载所有资源(即图像、js 和 css)。我不知道问题出在哪里,因此我在同一个网页上做了一个“页面另存为”,并比较了两个 .html 文件(我从浏览器保存的文件和我缓存的文件。)。

发现浏览器保存的html文件中,所有图片等文件源路径都被替换为本地路径了。。

<div><img src="./CNN.com International - Breaking, World, Business, Sports, Entertainment and Video News_files/footer_cnn_logo.png" width="23" height="11" alt="" border="0" class="cnn_ie6png">

但是我缓存的html文件,还是有网站的url..

<div><img src="http://i.cdn.turner.com/cnn/.e/img/3.0/global/footer/pngs/footer_cnn_logo.png" width="23" height="11" alt="" border="0" class="cnn_ie6png"/>

那么我该如何更改它..?或者将网页与所有资源一起保存的正确方法是什么..??

我正在保存所有文件,因为它是在storeCachedResponse:forRequest: 方法中获取的。那么我是否必须在保存之前对这个 .html 文件进行一些修改,以便它可以正确引用本地文件夹中的所有资源路径??

仅供参考,我不想使用 ASIWebPageRequest (Download and cache entire web page on ios)

///////////编辑//////////////p>

正如 Edwin Vermeer 所回答的,我能够从我的缓存中获取正确的本地文件并返回响应。但看起来, UIWebView 不理解该数据..! 这就是我在cachedResponseForRequest: 方法中返回响应的方式

if (shouldFetchFromCache) {
            NSData* content = [NSData dataWithContentsOfFile:storagePath];

            NSDictionary *headers = @{@"Access-Control-Allow-Origin" : @"*", @"Access-Control-Allow-Headers" : @"Content-Type"};
            NSHTTPURLResponse *urlresponse = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:headers];

            cachedresponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:content];
        }


return cachedresponse;!

这就是网页在 webView 上的加载方式..

Screenshot of the app

是否有一些不同的方式来返回响应...?顺便说一句,内容和响应都不是零。

请求url的一个例子是

Request : http://z.cdn.turner.com/cnn/tmpl_asset/static/intl_homepage/1021/js/intlhplib-min.js

其存储的文件路径是:

 file path: /Users/akshay/Library/Application Support/iPhone Simulator/5.1/Applications/86ED671C-AE45-449A-A2E1-D08281AB54CF/Documents/CVWebCache/cnn/tmpl_asset/static/intl_homepage/1021/js/intlhplib-min.js

【问题讨论】:

    标签: ios ios5 uiwebview asihttprequest nsurlcache


    【解决方案1】:

    如果您的自定义 NSUrlCache 仍然处于活动状态,则对资源的请求应该通过它。您可以在那里测试 URL 并从您的文档文件夹中返回正确的文件。

    然后您也可以打开原始 URL 而不是文档文件夹中的文件。再次,您的 NSUrlCache 将从您的文件夹返回文件。

    您不必创建 NSHTTPUrlResponse。一个 NSURLResponse 就足够了。

     if (shouldFetchFromCache) {
                NSData* content = [NSData dataWithContentsOfFile:storagePath];
    
                NSURLResponse* response = [[NSURLResponse alloc] initWithURL:request.URL MIMEType:@"cache" expectedContentLength:[content length] textEncodingName:nil] ;
    
                cachedresponse = [[NSCachedURLResponse alloc] initWithResponse:response data:content];
            }
        }
    
        return cachedresponse;
    

    【讨论】:

    • 你是对的..!我刚刚知道了..:) 我拥有所有文件,并且还在文档目录中获取了正确的文件路径,并且也从文件中获取了 NSData。但是 webView 没有正确解释该数据..!
    • 看看我在github.com/evermeer/EVURLCache 中是如何做到的,我已经将它与 UIWebview 一起使用。还要确保为保存在文档文件夹中的文件设置“不备份”属性。否则所有网页也会备份到 iCloud。
    • 我提到了相同的内容,并且几乎使用了它的大部分代码..!我已经编辑了我的问题..你能看一下吗..??
    • 您不必创建 NSHTTPUrlResponse。一个 NSURLResponse 就足够了。您可以使用类似: NSData* content = [NSData dataWithContentsOfFile:storagePath]; NSURLResponse* response = [[NSURLResponse alloc] initWithURL:request.URL MIMEType:@"cache" expectedContentLength:[content length] textEncodingName:nil] ; return [[NSCachedURLResponse alloc] initWithResponse:response data:content] ;
    • 你是对的..!!它现在起作用了..用那个更新答案..!!我会接受它..:) 谢谢..!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    相关资源
    最近更新 更多