Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, I found the solution.</p> <p>The idea is to use <code>connection:willCacheResponse:</code> method. Before cache the response it will be executed and there we can change response and return new, or return nil and the response will not be cached. As I use AFNetworking, there is a nice method in operation:</p> <pre><code>- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block; </code></pre> <p>Add code:</p> <pre><code> [operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) { if([connection currentRequest].cachePolicy == NSURLRequestUseProtocolCachePolicy) { cachedResponse = [cachedResponse responseWithExpirationDuration:60]; } return cachedResponse; }]; </code></pre> <p>Where <code>responseWithExpirationDuration</code> from category:</p> <pre><code>@interface NSCachedURLResponse (Expiration) -(NSCachedURLResponse*)responseWithExpirationDuration:(int)duration; @end @implementation NSCachedURLResponse (Expiration) -(NSCachedURLResponse*)responseWithExpirationDuration:(int)duration { NSCachedURLResponse* cachedResponse = self; NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)[cachedResponse response]; NSDictionary *headers = [httpResponse allHeaderFields]; NSMutableDictionary* newHeaders = [headers mutableCopy]; newHeaders[@"Cache-Control"] = [NSString stringWithFormat:@"max-age=%i", duration]; [newHeaders removeObjectForKey:@"Expires"]; [newHeaders removeObjectForKey:@"s-maxage"]; NSHTTPURLResponse* newResponse = [[NSHTTPURLResponse alloc] initWithURL:httpResponse.URL statusCode:httpResponse.statusCode HTTPVersion:@"HTTP/1.1" headerFields:newHeaders]; cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:newResponse data:[cachedResponse.data mutableCopy] userInfo:newHeaders storagePolicy:cachedResponse.storagePolicy]; return cachedResponse; } @end </code></pre> <p>So, we set expiration in seconds in http header according to <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">http/1.1</a> For that we need one of headers to be set up: Expires, Cache-Control: s-maxage or max-age Then create new cache response, because the properties is read only, and return new object.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload