On 29 Nov 2017, at 03:48, sebastienboisv...@yahoo.com wrote:

> I've set this up for our app that uses a group container, where the main app 
> and a helper app both use the same custom NSURLCache, and it does work

Yay!

> Is there a way to specify what headers are significant when requesting a 
> cached response from a NSURLCache?

No.

I had a complex spiel regarding workarounds (I plonked it at the end of this 
email, just so it’s not lost) but I figured I should check that the 
`User-Agent` string is the problem here.  And I’m not sure it is.  Consider 
this code:

---------------------------------------------------------------------------
let url = URL(string: "http://example.com";)!
var req1 = URLRequest(url: url)
req1.setValue("agent1", forHTTPHeaderField: "User-Agent")

var req2 = URLRequest(url: url)
req2.setValue("agent2", forHTTPHeaderField: "User-Agent")

let res1 = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", 
headerFields: [
    "Date": "Wed, 29 Nov 2017 08:33:15 GMT",
    "Server": "Apache",
    "Last-Modified": "Mon, 11 Mar 2013 21:44:16 GMT",
    "ETag": "\"606-4d7ad13106c00\"",
    "Accept-Ranges": "bytes",
    "Content-Length": "18",
    "Connection": "close",
    "Content-Type": "text/html",
])!
let cres1 = CachedURLResponse(response: res1, data: "Hello Cruel 
World!".data(using: .utf8)!)

URLCache.shared.storeCachedResponse(cres1, for: req1)

let cres2 = URLCache.shared.cachedResponse(for: req2)
---------------------------------------------------------------------------

`cres2` comes back as non-nil, even though `req2` has a different `User-Agent` 
header than the `req1` that was used to store the response.  So, are you sure 
that the `User-Agent` string is the problem here?

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

---------------------------------------------------------------------------
The most obvious solution here is to have both the app and the helper set the 
same `User-Agent` string.  That seems reasonable given that one is embedded 
within the other.  Why not do that?

If you have to keep these separate then there other games you can play.  For 
example, you could:

1. Implement `-URLSession:dataTask:willCacheResponse:completionHandler:` and 
normalise the `User-Agent` string in the cached response.

2. Initially try to fetch the request from the cache with the normalised 
`User-Agent` string and the `NSURLRequestReturnCacheDataDontLoad` cache policy. 
 If it loads, you’ll then have to check the age yourself.  If it doesn’t load, 
or its too old, you can issue a new load request with the default cache policy.
---------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (Macnetworkprog@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/macnetworkprog/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to