Loading a cached web page using WebKit and NSURLRequest?

I found this codes at Apple’s mailing list.

// Create a NSURLRequest:
NSURLRequest *myRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.apple.com";] 

// Create your own NSCachedURLResponse and add it to the cache:
NSData *myData = ... // the HTML you wish to put in WebKit's cache as 'www.apple.com'
NSURLResponse *responseToCache = [[NSURLResponse alloc] initWithURL:[NSURL URLWithString:@"http://www.apple.com";] 
                  expectedContentLength:[myData length]

NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:responseToCache 
[[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:myRequest];

// Check if it's really there; I'm using myRequest again, but it can just as easily be
// another NSURLRequest with the same URL and cache-friendly policy:
NSLog(@"cache=%@", [[NSURLCache sharedURLCache] cachedResponseForRequest:myRequest]);

// Have your WebView load the web page using the initial request.
[[myWebView mainFrame] loadRequest:myRequest];

Can this solve my problem? I should try it.


One response to this post.

  1. Hi Jong, just wondering if this is applicable to normal NSData you get when downloading data from a online service?

    for instance can i download random data, cache it then do what i want with that data using this example?

    I am going to try.. it looks similar to what i have already regarding nsurlrequest etc.. just i am missing the cache part.. I will let you know how I go.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: