質問

I subclassed NSURLCache and overriden – cachedResponseForRequest: and – storeCachedResponse:forRequest: . I am storing all the files am getting in the application's documents directory.

Now, the problem is, the .html file when opened doesn't load all the resources (i.e, images,js and css). I didn't know wat the problem was, hence I did a "save page as" on the same web page and compared both the .html files (the one i saved from the browser and the one i cached.).

I found that in the html file saved from the browser, all the images and other files source paths have been replaced with the local paths..

<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">

But the html file that i cached, still has the url of the website..

<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"/>

So how do I change it..?or what's the right way to save a web page along with all the resources..??

AM saving all the files am getting in storeCachedResponse:forRequest: method as it is. So do I have to do some modifications to this .html file before saving, so that it would refer all the resource path in the local folder properly ??

FYI, I do not want to use ASIWebPageRequest (Download and cache entire web page on ios)

//////////// Edit ////////////

So as Edwin Vermeer has answered, am able to get the right local file from my cache and return the response. but looks like, the UIWebView doesn't understand that data..! This is how am returning the response in cachedResponseForRequest: method

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;!

This is how the web page loads on the webView..

Screenshot of the app

Is there some different way to return the response...? by the way, neither content or the response are nil.

an example of the request url is

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

and the file path where its stored is :

 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
役に立ちましたか?

解決

If your custom NSUrlCache is still active, then the requests for the resources should go past it. You could there test for the URL and return the correct file from your documents folder.

You could then also open the original URL instead of the file from the documents folder. Again your NSUrlCache will return the file from your document folder.

You do not have to create a NSHTTPUrlResponse. A NSURLResponse is good enough.

 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;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top