【问题标题】:Offline mode is not working when using NSURLRequestReturnCacheDataDontLoad使用 NSURLRequestReturnCacheDataDontLoad 时离线模式不起作用
【发布时间】:2012-05-15 01:38:10
【问题描述】:

我目前是 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];
}*

【问题讨论】:

    标签: iphone objective-c ios ios5


    【解决方案1】:

    很遗憾,UIWebView 不会保留网页缓存。我从未这样做过,但使用 AFCache 应该可以用于缓存持久化。

    还要确保缓存页面到位。如果网页不可缓存(即使用 Pragma: no-cache),UIWebView 可能仍然无法缓存它。

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      相关资源
      最近更新 更多