我目前是 iOS 开发的新手,正在尝试获取缓存策略 (NSURLRequestReturnCacheDataDontLoad) 以在 iPhone 离线(使用飞行模式)时从缓存中提取以查看网页.我目前正在使用 Apple 提供的 Reachability API 来确定网络/wifi 连接是否正常运行。这工作正常但是当我进入飞行模式时我没有得到一个网页来填充 UIWebView。任何建议都会非常有帮助,我在网上四处看看,但没有找到太多有用的链接。谢谢。
代码如下:
*- (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];
}*
最佳答案
不幸的是,UIWebView
不会保留网页缓存。我从来没有这样做过,但使用 AFCache应该用于缓存持久化。
还要确保您已准备好缓存页面。如果网页不可缓存(即使用 Pragma: no-cache),UIWebView
可能仍然不会缓存它。
关于iphone - 使用 NSURLRequestReturnCacheDataDontLoad 时离线模式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10593147/