为AFNetworking 3.x实现自定义缓存
嗨,我正在编写IOS api,使用AFNetworking 3.x从服务器获取数据我的服务器不支持缓存。 这个服务器头
Connection →Keep-Alive Content-Length →4603 Content-Type →text/html; charset=UTF-8 Date →Wed, 10 Aug 2016 04:03:56 GMT Keep-Alive →timeout=5, max=100 Server →Apache/2.4.18 (Unix) OpenSSL/1.0.1e-fips PHP/5.4.45 X-Powered-By →PHP/5.4.45
我想构建自定义API以启用缓存(例如,请求和响应应该缓存10秒,这样如果用户发出相同的请求,则使用缓存数据。如果缓存已过期的应用程序将再次重新下载)
我的AFNetworking
Singleton
#pragma mark - Shared singleton instance + (ApiClient *)sharedInstance { static ApiClient *sharedInstance = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:SINGPOST_BASE_URL] sessionConfiguration:sessionConfiguration]; return sharedInstance; } - (id)initWithBaseURL:(NSURL *)url { if ((self = [super initWithBaseURL:url])) { self.requestSerializer = [AFHTTPRequestSerializer serializer]; } return self; }
我的JSON请求
- (void)sendJSONRequest:(NSMutableURLRequest *)request success:(void (^)(NSURLResponse *response, id responseObject))success failure:(void (^)(NSError *error))failure { self.responseSerializer.acceptableContentTypes = [AFHTTPResponseSerializer serializer].acceptableContentTypes; self.requestSerializer.timeoutInterval = 5; self.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad; [self setDataTaskWillCacheResponseBlock:^NSCachedURLResponse * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSCachedURLResponse * _Nonnull proposedResponse) { return [[NSCachedURLResponse alloc] initWithResponse:proposedResponse.response data:proposedResponse.data userInfo:proposedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed]; }]; NSURLSessionDataTask *dataTask = [ApiClient.sharedInstance dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { NSLog(@"Error URL: %@",request.URL.absoluteString); NSLog(@"Error: %@", error); failure(error); } else { NSDictionary *jsonDict = (NSDictionary *) responseObject; success(response,jsonDict); NSLog(@"Success URL: %@",request.URL.absoluteString); NSLog(@"Success %@",jsonDict); } }]; [dataTask resume]; }
如何修改波纹管代码以启用缓存请求和响应(我希望响应保持10秒)(如果没有互联网连接则为1天)任何帮助都非常感谢。 谢谢!
[self setDataTaskWillCacheResponseBlock:^NSCachedURLResponse * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSCachedURLResponse * _Nonnull proposedResponse) { return [[NSCachedURLResponse alloc] initWithResponse:proposedResponse.response data:proposedResponse.data userInfo:proposedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed]; }];
代替
return [[NSCachedURLResponse alloc] initWithResponse:proposedResponse.response
做
NSMutableDictionary *dictionary = [proposedResponse.response.allHeaderFields mutableCopy]; dictionary[@"Cache-Control" = ... NSHTTPURLResponse *modifiedResponse = [[NSHTTPURLResponse alloc initWithURL:proposedResponse.response.URL statusCode:proposedResponse.response.statusCode HTTPVersion:@"HTTP/1.1" headerFields:modifiedHeaders]; [modifiedResponse return [[NSCachedURLResponse alloc] initWithResponse:modifiedResponse
请注意,无法从原始请求中检索HTTP版本。 我很确定我提出了一个错误。 我也很确定它对任何东西都没有任何影响,我认为它甚至不会存储在底层对象中,所以它可能并不重要。
- ios paypal与多个对象的简单付款
- 当我收到消息iPhone应用程序时,自动填充OTP到TextField
- 我可以从NSHTTPURLResponse中的HTTP状态行访问“Reason Phrase”(原因短语)吗?
- iOSアプリの启动速度を2倍にするために,复数のDynamic FrameworkをStaticひ,ひとつのDynamic Frameworkを作るwith Swift
- 如何在我的iPod touch上安装iPhone应用程序
- FairPlay Streaming:在AVPlayerItemVideoOutput上调用copyPixelBufferForItemTime将返回NULL
- 在XCode 7.1的UItesting中访问应用程序模块时链接器错误
- iOS应用程序只在模拟器中显示一半
- 在swift静态错误中自定义UIViewanimation