klc*_*r89 5 iphone objective-c uiwebview ios ios7
我在我的应用程序中发现了一个有UIWebView的问题.iOS 7缓存空白主体304响应,导致在用户刷新UIWebView时显示空白页面.这不是很好的用户体验,我试图弄清楚如何在iOS端解决这个问题,因为我无法控制Amazon S3如何响应标头(这是我用于资源托管的人).
这些人发现了这个错误的更多细节:http://tech.vg.no/2013/10/02/ios7-bug-shows-white-page-when-getting-304-not-modified-from-server /
我很感激任何帮助,我可以在应用程序方面而不是服务器方面解决这个问题.
谢谢.
更新:使用赏金的建议作为指导来修复此错误:
@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;
}
Run Code Online (Sandbox Code Playgroud)
由于其他问题是每次都使用 NSURLConnection,这似乎有点开销:为什么不在页面加载(完整或不完整)后执行一个小的 javascript 来告诉您页面是否实际显示?查询应该存在的标签(比如您的内容 div)并使用
[UIWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('adcHeader')!=null"]
Run Code Online (Sandbox Code Playgroud)
然后,如果返回 false,您可以使用您自己描述的缓存破坏技术手动重新加载 URL:
NSString *uniqueURL = [NSString stringWithFormat:@"%@?t=%d", self.url, [[NSDate date] timeIntervalSince1970]];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:uniqueURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0]];
Run Code Online (Sandbox Code Playgroud)
[编辑]
根据评论中的讨论和其他一些答案,我认为您可能有最好的解决方案手动更改NSURLCache.
根据我收集的信息,您主要试图解决重新加载/重新显示场景。在这种情况下,请查询NSURLCache是否有正确的响应,如果没有,请在重新加载之前删除存储的值UIWebView。
[编辑2]
根据您的新结果,尝试NSURLCache在损坏时删除它:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
我们可能必须改进缓存是否再次无效的检查,但理论上这应该可以解决问题!
| 归档时间: |
|
| 查看次数: |
3507 次 |
| 最近记录: |