iOS 7 UIWebView 304缓存bug,空白页面 [英] iOS 7 UIWebView 304 cache bug, blank pages

查看:110
本文介绍了iOS 7 UIWebView 304缓存bug,空白页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中发现了一个有UIWebView的问题。 iOS 7缓存空白主体304响应,导致在用户刷新UIWebView时显示空白页面。这不是很好的用户体验,我试图弄清楚如何在iOS端解决这个问题,因为我无法控制Amazon S3如何响应标头(这是我用于资源托管的人)。

I have a problem I have discovered in my app that has a UIWebView. iOS 7 caches a blank body 304 response, resulting in blank pages being shown when the user refreshes the UIWebView. This is not good user expierience and I'm trying to figure out how to solve this on the iOS side, as I do not have control over how Amazon S3 responds to headers (that's who I use for my resource hosting).

这些人发现了这个错误的更多细节: http://tech.vg.no/2013/10/02/ios7-bug-shows-white-page -when-getting-304-not-modified-from-server /

More details of this bug were found by these people: http://tech.vg.no/2013/10/02/ios7-bug-shows-white-page-when-getting-304-not-modified-from-server/

我很感激能为我如何解决这个问题提供帮助一边而不是服务器端。

I'd appreciate any help offered to how I can solve this on the app side and not the server side.

谢谢。

更新:使用赏金的建议修复此错误指南:

Update: fixed this bug using the bounty's suggestion as a guideline:

@property (nonatomic, strong) NSString *lastURL;

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if ([self.webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"].length < 1)
    {
        NSLog(@"Reconstructing request...");
        NSString *uniqueURL = [NSString stringWithFormat:@"%@?t=%@", self.lastURL, [[NSProcessInfo processInfo] globallyUniqueString]];
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:uniqueURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0]];
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    self.lastURL = [request.URL absoluteString];
    return YES;
}


推荐答案

正如其他问题是每次使用NSURLConnection,这似乎有点开销:
为什么不在页面加载(完整或不完整)之后执行一个小的javascript,它可以告诉你页面是否实际显示?查询应该存在的标记(比如你的内容div)并使用

As the other questions are to use the NSURLConnection every time, which seems like a bit of an overhead: Why don't you execute a small javascript after the page was loaded (complete or incomplete) that can tell you if the page is actually showing? Query for a tag that should be there (say your content div) and give a true/false back using the

[UIWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('adcHeader')!=null"]

然后,是否应该返回false,你可以使用你自己描述的缓存破坏技术手动重新加载URL:

And then, should that return false, you can reload the URL manually using the cache-breaker technique you described yourself:

NSString *uniqueURL = [NSString stringWithFormat:@"%@?t=%d", self.url, [[NSDate date] timeIntervalSince1970]]; 
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:uniqueURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0]];

[edit]

基于在评论中讨论和其他一些答案,我认为您可能有最佳解决方案手动更改 NSURLCache

based on the discussion in the comments and some of the other answers, I think you might have the best solution manually changing the NSURLCache.

从我收集的内容来看,你主要是尝试解决重载/重播方案。在这种情况下,如果有正确的响应,则查询 NSURLCache ,如果没有,则在重新加载 UIWebView 之前删除storedvalue 。

From what I gathered, you're mainly trying to solve a reload/reshow scenario. In that case, query the NSURLCache if a correct response is there, and if not delete the storedvalue before reloading the UIWebView.

根据您的新结果,尝试删除 NSURLCache 当它被破坏时:

based on your new results, try to delete the NSURLCache when it is corrupted:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache]cachedResponseForRequest:request];

  if (cachedResponse != nil && [[cachedResponse data] length] > 0)
  {
      NSLog(@"%@",cachedResponse.response);
  } else {
    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
  }

  return YES;
}

如果缓存再次无效,我们可能需要优化检查,但是理论上应该这样做!

We might have to refine the check if the cache is invalid again, but in theory this should do the trick!

这篇关于iOS 7 UIWebView 304缓存bug,空白页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆