iphone - 使用 NSURLRequestReturnCacheDataDontLoad 时离线模式不起作用

标签 iphone objective-c ios ios5

我目前是 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/

相关文章:

iphone - 将 NIB 文件加载到 UIView 时遇到问题

iphone - 拉动刷新 (ios)

iPhone:如何在没有淡入淡出效果的情况下执行 kCATransitionMoveIn

iphone - 无法读取新 iPhone 的符号,适用于运行 iOS 5 的 iPad

ios - UIAlertController 未从 AppDelegate 显示

objective-c - iOS动态添加UIControls

ios - Alamofire 当数据响应 Swift 4 定时器

c++ - 我可以从结构指针得到什么?

ios - RevealViewController 为 nil,适用于其他文件

iphone - iPhone 应用程序的财务图表