I am using URLSession with the default useProtocolCachePolicy cache policy. Using a proxy such as Charles I can verify that the caching is working as expected for a request with an Etag where the server is returning a HTTP response of 304 (Not Modified). However the URLSession caching is hiding this response code and instead executing my request completion handlers with a status code of 200 with the cached data. I understand this is as expected as the Foundation libraries are encapsulating this behaviour. However I have a scenario that requires a UI change on a 304 response code. Is there any delegate or handler I can use to get a peek in to the actual status code before the Foundation libraries do their magic?
One possible workaround is as @ChanchalChauhan has hinted, forcing URLRequest to ignore the local cache (by setting the cache policy to .reloadIgnoringLocalCacheData) and manually added the If-None-Match HTTP header with the value extracted from the CachedURLResponse retrieved from the URLCache for the request. However to make this solution complete this logic must only be performed if the cached response has expired, as otherwise a network request is not required. I have not found a way to determine if the cached value has expired.