gpt4 book ai didi

iOS 7 UIWebView 304 缓存错误,空白页面

转载 作者:可可西里 更新时间:2023-11-01 04:44:03 28 4
gpt4 key购买 nike

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

这些人发现了此错误的更多详细信息: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;
}

最佳答案

你可以实现一个NSURLProtocol,然后在+canonicalRequestForRequest:中修改请求来覆盖缓存策略。

这将适用于所有发出的请求,包括 Web View 中的静态资源,这些资源通常不会与公共(public) API 委托(delegate)协商。

这非常强大,但也很容易实现。

这里有更多信息: http://nshipster.com/nsurlprotocol/

引用: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSURLProtocol_Class/Reference/Reference.html


这是一个例子:

@interface NoCacheProtocol : NSURLProtocol

@end

@implementation NoCacheProtocol

+ (void)load
{
[NSURLProtocol registerClass:[NoCacheProtocol class]];
}

+ (BOOL)canInitWithRequest:(NSURLRequest*)theRequest
{
if ([NSURLProtocol propertyForKey:@“ProtocolRequest” inRequest:theRequest] == nil) {
return YES;
}
return NO;
}

+ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)theRequest
{
NSMutableURLRequest* request = [theRequest mutableCopy];
[request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
//Prevent infinite recursion:
[NSURLProtocol setProperty:@YES forKey:@"ProtocolRequest" inRequest:request];

return request;
}

- (void)startLoading
{
//This is an example and very simple load..

[NSURLConnection sendAsynchronousRequest:self.request queue:[NSOperationQueue currentQueue] completionHandler:^ (NSURLResponse* response, NSData* data, NSError* error) {
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];
}];
}

- (void)stopLoading
{
NSLog(@"something went wrong!");
}

@end

关于iOS 7 UIWebView 304 缓存错误,空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21768242/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com