I notice that the iphone safari caches content so that your page load for later is much faster much like a desktop browser. So take mobile gmail web page for example, the first load is quite slow (5-10 secondS). But if I close the tab and reopen the page again, it's very quick (1 second).
However, this behavior is not the same if you load the content through a UIWebView in your app. Am I missing some settings? How do I make the UIWebView cache the content automatically without going through the hassle of saving the content myself?
To clear old contents of webview With UIWebView you would use UIWebViewDelegate 's - webViewDidFinishLoad: .
The key is: NSURLRequestReturnCacheDataElseLoad
NSData *urlData; NSString *baseURLString = @"mysite.com"; NSString *urlString = [baseURLString stringByAppendingPathComponent:@"myfile"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0]; NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:nil]; if (connection) { urlData = [NSURLConnection sendSynchronousRequest: request]; NSString *htmlString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; [webView loadHTMLString:htmlString baseURL:baseURLString]; [htmlString release]; } [connection release];
NSString *stringurl=[NSString stringWithFormat:@"http://www.google.com"]; NSURL *url=[NSURL URLWithString:stringurl]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:15.0]; [uiwebview loadRequest:theRequest];
It will load a url first time then looks for for only the content changes..,if there is no updates in the url content it will load from the cache(local storage).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With