【问题标题】:Why is UIWebView not loading data from cache on iOS4?为什么 UIWebView 不从 iOS4 上的缓存中加载数据?
【发布时间】:2012-05-08 03:57:24
【问题描述】:

大家好,在我的 APP 中,我通过继承 NSURLCache 类创建了自定义缓存类,并实现了将本地文件数据作为缓存数据传递所需的方法。I have done it same as this tutorial

在我的 APP 中有一个刷新按钮,点击它我必须刷新当前页面,为此我已经完成以下操作以加载本地存储的 CSS 和 JS 文件

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if (isGoingToRefresh) {
        [NSURLCache setSharedURLCache:cache];
        isGoingToRefresh = NO;
        return YES;
    }
    NSString *path = [mainWebView stringByEvaluatingJavaScriptFromString:@"window.location.pathname"];
    path = [NSString stringWithFormat:@"%@%@",[[[AppDelegate sharedAppDelegate]configurationFile]objectForKey:@"RootURL"],path];
    [NSURLCache setSharedURLCache:cache];
    if ([path isEqualToString:[[request URL]absoluteString]]) {
        [self showRefreshLoader];
        isGoingToRefresh = YES;
        NSURLRequest *localRequest = [NSURLRequest requestWithURL:[request URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:240] ;
        [webView loadRequest:localRequest];
        return NO;
    }

    return YES;

}

上面的代码通过调用下面的 NSURLCache 方法从缓存中加载数据 -

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request

但是在 iOS4 webView 上没有调用上面的 NSURLCache 方法。

我不明白为什么请给我一些好的解决方案。

【问题讨论】:

    标签: iphone objective-c ios5 ios4 uiwebview


    【解决方案1】:

    尝试使用来提出您的要求

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url 
                                                cachePolicy:NSURLCacheStorageAllowed 
                                            timeoutInterval:60];
    

    【讨论】:

    • 它与以前的解决方案相同(无论我做了什么)......,我试过它不起作用感谢回复......
    • 这是不同的缓存策略。
    • 在 iOS 5 上它适用于缓存策略 NSURLRequestReturnCacheDataElseLoad 但不适用于 iOS4,我已经尝试了 1/2 以上,现在没有人解决我的问题我打算改变缓存方式... ....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2014-11-26
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多