This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On iOS 7, the protocol is required to *always* load its own cache item, even for the NSURLRequestReturnCacheDataDontLoad and NSURLRequestReturnCacheDataElseLoad policies. This applies to both NSURLConnection and NSURLSession. Found by unit testing on iOS 7 simulator with XCode 6.4.
- Loading branch information
Showing
1 changed file
with
32 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,8 +353,15 @@ - (NSCachedURLResponse *)loadCachedResponseIfAllowed | |
// We're making some choices here to limit the surface area of caching, given we don't yet | ||
// have a fully-featured client caching implementation (missing sufficient validity checks). | ||
// | ||
// For the default request cache policy (NSURLRequestUseProtocolCachePolicy), we have to load | ||
// the cache ourselves. We're applying the following rules in that case: | ||
// On iOS 8 and 9, the NSURL loading system will supply the cached item to our constructor | ||
// for NSURLRequestReturnCacheDataElseLoad and NSURLRequestReturnCacheDataDontLoad. For | ||
// NSURLRequestUseProtocolCachePolicy, we have to load it ourselves. | ||
// | ||
// On iOS 7, the NSURL loading system never supplies the cached item to our constructor. | ||
// We have to load it ourselves for NSURLRequestReturnCacheDataElseLoad, | ||
// NSURLRequestReturnCacheDataDontLoad, and NSURLRequestUseProtocolCachePolicy. | ||
|
||
// For NSURLRequestUseProtocolCachePolicy, we're applying the following rules regarding loading: | ||
// - NSURLConnection-based requests will not support caching. | ||
// - NSURLSession-based requests must set the SPDYURLSession property on the request, and | ||
// must provide a NSURLCache in their NSURLSessionConfiguration. There is no fallback to | ||
|
@@ -363,19 +370,38 @@ - (NSCachedURLResponse *)loadCachedResponseIfAllowed | |
// | ||
// This behavior may change in the future. | ||
|
||
NSCachedURLResponse *response; | ||
// version 7 and lower will be represented as 0 | ||
static NSInteger osVersion; | ||
static dispatch_once_t once; | ||
dispatch_once(&once, ^{ | ||
NSProcessInfo *processInfo = [NSProcessInfo processInfo]; | ||
if ([processInfo respondsToSelector:@selector(operatingSystemVersion)]) { | ||
osVersion = [processInfo operatingSystemVersion].majorVersion; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
} else { | ||
osVersion = 0; | ||
} | ||
}); | ||
|
||
BOOL isNSURLSession = (_associatedSession != nil || | ||
_associatedSessionTask != nil || | ||
([self respondsToSelector:@selector(task)] && self.task != nil)); | ||
if (isNSURLSession) { | ||
This comment has been minimized.
Sorry, something went wrong.
NSProgrammer
Collaborator
|
||
NSURLSessionConfiguration *config = _associatedSession.configuration; | ||
if (config.requestCachePolicy == NSURLRequestUseProtocolCachePolicy) { | ||
response = [config.URLCache cachedResponseForRequest:self.request]; | ||
NSURLRequestCachePolicy cachePolicy = config.requestCachePolicy; | ||
if (cachePolicy == NSURLRequestUseProtocolCachePolicy || | ||
(osVersion < 8 && (cachePolicy == NSURLRequestReturnCacheDataDontLoad || cachePolicy == NSURLRequestReturnCacheDataElseLoad))) { | ||
return [config.URLCache cachedResponseForRequest:self.request]; | ||
} | ||
} else { | ||
// NSURLConnection on iOS 7 forces us to always load the cache item. But we don't want to | ||
// do that for NSURLRequestUseProtocolCachePolicy. | ||
NSURLRequestCachePolicy cachePolicy = self.request.cachePolicy; | ||
if (osVersion < 8 && (cachePolicy == NSURLRequestReturnCacheDataDontLoad || cachePolicy == NSURLRequestReturnCacheDataElseLoad)) { | ||
return [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request]; | ||
} | ||
} | ||
|
||
return response; | ||
return nil; | ||
} | ||
|
||
- (instancetype)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client | ||
|
this isn't considerate of Mac OS X support