Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did iOS 8 break NSURLCache?

Long story short, I just updated to Xcode 6 to check how my app works on iOS 8. I noticed that it doesn't use the cache even though it should. I'm using AFNetworking setting the cachePolicy like this:

sessionManager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;

I still have an iOS 7 device in which I tested the same code and it works there as expected.

Does anyone have a solution for this, or do we need to wait for Apple to fix it?

like image 610
Pahnev Avatar asked Sep 21 '14 23:09

Pahnev


People also ask

What happened to iOS 8 1 1?

iOS 8.0.1 was released on September 24, 2014, as the first update to iOS 8. The update was meant to bring various bug fixes; however, the update was withdrawn due to an issue that disabled Touch ID and cellular network connectivity on some models.

What's new in iOS 14 8 8?

The second security hole fixed in iOS 14.8 is in the Apple WebKit browser engine, where processing malicious web content could allow an adversary to execute code. Apple believes both vulnerabilities have been exploited by attackers, so it recommends you install iOS 14.8 now.

When did iOS 8 come out?

iOS 8 is the eighth major release of the iOS mobile operating system developed by Apple Inc., being the successor to iOS 7. It was announced at the company's Worldwide Developers Conference on June 2, 2014, and was released on September 17, 2014. It was succeeded by iOS 9 on September 16, 2015.

Will downloading iOS 8 2 Break my iPhone?

^ Langley, Hugh (September 26, 2014). "iOS 8.0.2 is now available to download, and this one shouldn't break your iPhone". TechRadar. Future plc.


1 Answers

I'm almost certain that iOS 8.0 has broken NSURLSession's ability to cache HTTP response data. I've opened a radar with Apple about this issue.

Here's some sample code I wrote to prove this:

NSURLSession *session = [NSURLSession sharedSession];
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                     diskCapacity:32 * 1024 * 1024
                                                         diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURL *URL = [NSURL URLWithString:@"http://i.imgur.com/b5pyONe.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL
                                         cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                     timeoutInterval:5];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Error occurred");
    } else {
        NSLog(@"Fetched resource!");
    }
    dispatch_semaphore_signal(semaphore);
}];
[task resume];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

request = [NSURLRequest requestWithURL:URL
                           cachePolicy:NSURLRequestReturnCacheDataDontLoad
                       timeoutInterval:5];
task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Something bad happened: %@", error);
    } else {
        NSLog(@"Fetched resource!");
    }
}];
[task resume];

Even creating your own NSURLSession -- with an NSURLSessionConfiguration that has an NSURLCache that you create yourself -- won't fix this issue. For now, if you need cached responses badly, you have to use NSURLConnection.

like image 82
Richard Shin Avatar answered Oct 05 '22 23:10

Richard Shin