Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhenakh/11188663 to your computer and use it in GitHub Desktop.
Save akhenakh/11188663 to your computer and use it in GitHub Desktop.
Force private cache-control requests to be public (cachable) with AFHTTPSessionManager
[sessionManager
setDataTaskWillCacheResponseBlock:^NSCachedURLResponse *
(NSURLSession *session,
NSURLSessionDataTask *dataTask,
NSCachedURLResponse *
proposedResponse) {
NSURLResponse *response = proposedResponse.response;
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *headers = HTTPResponse.allHeaderFields;
NSCachedURLResponse *cachedResponse;
if (headers[@"Cache-Control"]) {
NSMutableDictionary *modifiedHeaders = headers.mutableCopy;
modifiedHeaders[@"Cache-Control"] = [modifiedHeaders[@"Cache-Control"]
stringByReplacingOccurrencesOfString:@"private"
withString:@"public"];
NSHTTPURLResponse *modifiedResponse =
[[NSHTTPURLResponse alloc] initWithURL:HTTPResponse.URL
statusCode:HTTPResponse.statusCode
HTTPVersion:@"HTTP/1.1"
headerFields:modifiedHeaders];
cachedResponse = [[NSCachedURLResponse alloc]
initWithResponse:modifiedResponse
data:proposedResponse.data
userInfo:proposedResponse.userInfo
storagePolicy:proposedResponse.storagePolicy];
} else {
cachedResponse = proposedResponse;
}
return cachedResponse;
}];
return sessionManager;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment