I am currently a newbie to iOS development and trying to get the cache policy (NSURLRequestReturnCacheDataDontLoad) to pull from the cache to view a web page while being in offline on the iPhone (using airplane mode). I am currently using the Reachability API provided by Apple to figure out if the network/wifi connection is up and running or not. This works fine but when I go to airplane mode I am not getting a webpage to populate the UIWebView. Any suggestions would very helpful, I look around online but did not find much helpful links. Thanks.
The code is below:
*- (IBAction)refresh:(id)sender
{
NSURL *url = [NSURL URLWithString:_entry.articleUrl];
NSURLRequest *webRequest = nil;
NSString *articleUrl = [_entry.articleUrl substringFromIndex:7];
NSArray *myArray = [articleUrl componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
NSString *hostUrl = [myArray objectAtIndex:0];
NSLog(@"%@", hostUrl);
Reachability *reachability = [Reachability reachabilityWithHostName:hostUrl];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
[[NSURLCache sharedURLCache] setMemoryCapacity:1024*1024*10];
// Verify current help file cache if we have network connection...
if (netStatus == ReachableViaWWAN || netStatus == ReachableViaWiFi)
{
webRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
}
else
{
// Network NOT reachable - show (local) cache if it exists
webRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval:30];
}
[_webView loadRequest:webRequest];
}*
Unfortunately,
UIWebViewdoes not persist web page cache. I have never done this but using AFCache should work for cache persisting.Also make sure you got the cached page in place. If the webpage is not cacheable (i.e. with Pragma: no-cache),
UIWebViewmight still not cache it.