Last active
August 29, 2015 14:00
-
-
Save akhenakh/11188663 to your computer and use it in GitHub Desktop.
Force private cache-control requests to be public (cachable) with AFHTTPSessionManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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