我正在尝试使用以下代码确保我始终拥有 UIWebView 的最新缓存副本:
// Set URL to help file on server
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", HELP_FILE_URL]];
// Check network reachability
wifiReach = [Reachability reachabilityWithHostName:[NSString stringWithFormat:@"%@", SERVER_URL]];
netStatus = [wifiReach currentReachabilityStatus];
// Start activity indicators
[self startAnimation];
// Verify current help file cache if we have network connection...
if (netStatus == ReachableViaWWAN || netStatus == ReachableViaWiFi) {
helpFileRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:30];
} else {
// Network NOT reachable - show (local) cache if it exists
helpFileRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval:30];
}
// Show help file in a web view
[webView loadRequest:helpFileRequest];
它在大多数情况下都可以正常工作,除非我在不终止应用程序的情况下进入飞行模式。进入飞行模式后,缓存的 webView 显示良好,但 UIWebView 委托
(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
也触发了我不想要的。我只希望在缓存为空时触发它!我怎样才能做到这一点?(如果我终止应用程序,它可以正常工作。)这是一个小细节,但我真的希望它能够正常工作:)