Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well I've finally reached a not so ugly workaround:</p> <p><strong>First</strong></p> <p>If you're using IOS5/IOS6 you can drop SDURLCache and use the native one:</p> <pre><code>//Set Cache NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil]; [NSURLCache setSharedURLCache:URLCache]; </code></pre> <p>But remember that in IOS5 https requests wont be cached in IOS6 they will.</p> <p><strong>Second</strong></p> <p>We need to add the following frameworks to our <code>Prefix.pch</code> so AFNetworking can start monitoring our internet connection.</p> <pre><code>#import &lt;MobileCoreServices/MobileCoreServices.h&gt; #import &lt;SystemConfiguration/SystemConfiguration.h&gt; </code></pre> <p><strong>Third</strong></p> <p>We need and AFHTTPClient instance so we can intercept every outgoing request and change his <code>cachePolicy</code></p> <pre><code>-(NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters { NSMutableURLRequest * request = [super requestWithMethod:method path:path parameters:parameters]; if (request.cachePolicy == NSURLRequestUseProtocolCachePolicy &amp;&amp; self.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { request.cachePolicy = NSURLRequestReturnCacheDataDontLoad; } if (self.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown) { puts("uknown reachability status"); } return request; } </code></pre> <p>With these peaces of code we can now detect when the wifi/3g is unavailable and the specify the request to use always the cache no matter what. (Offline Mode)</p> <p><strong>Notes</strong></p> <ul> <li><p>I still don't know what to do when the <code>networkReachabilityStatus</code> is <code>AFNetworkReachabilityStatusUnknown</code> This can happen is a request is made as soon as the application starts and AF has not obtained the internet status yet.</p></li> <li><p>Remember that in order for this to work the server has to set the correct cache headers in the http response.</p></li> </ul> <p><strong>UPDATE</strong></p> <p>Looks like IOS6 has some problems loading cached responses in no-internet situations, so even if the request is cached and the request cache policy is seted to <code>NSURLRequestReturnCacheDataDontLoad</code> the request will fail.</p> <p>So an ugly workaround is to modify <code>(void)connection:(NSURLConnection __unused *)connection didFailWithError:(NSError *)error</code> in <code>AFURLConnectionOperation.m</code> to retrieve the cached response if the request fails but only for specific cache policies.</p> <pre><code>- (void)connection:(NSURLConnection __unused *)connection didFailWithError:(NSError *)error { self.error = error; [self.outputStream close]; [self finish]; self.connection = nil; //Ugly hack for making the request succeed if we can find a valid non-empty cached request //This is because IOS6 is not handling cache responses right when we are in a no-connection sittuation //Only use this code for cache policies that are supposed to listen to cache regarding it's expiration date if (self.request.cachePolicy == NSURLRequestUseProtocolCachePolicy || self.request.cachePolicy == NSURLRequestReturnCacheDataElseLoad || self.request.cachePolicy == NSURLRequestReturnCacheDataDontLoad) { NSCachedURLResponse * cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request]; if (cachedResponse.data.length &gt; 0) { self.responseData = cachedResponse.data; self.response = cachedResponse.response; self.error = nil; } } } </code></pre>
    singulars
    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