为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版本。 我很确定我提出了一个错误。 我也很确定它对任何东西都没有任何影响,我认为它甚至不会存储在底层对象中,所以它可能并不重要。