NSURLCache
NSURLCache provides a composite in-memory and on-disk caching mechanism for URL requests to your application. As part of Foundation’s URL Loading System, any request loaded through NSURLConnection will be handled by NSURLCache.
NSURLCache is the built-in, automatic HTTP(S) cache used by NSURLConnection, NSURLSession (default/ephemeral configs), and WKWebView.
Key points:
- Default cache is tiny (almost no disk space). Always set a proper shared cache at launch (e.g. 20 MB memory + 100 MB disk).
- Caching is controlled by the request’s
cachePolicyand the server’s HTTP headers (Cache-Control,ETag,Last-Modified,Expires). - Server headers win: if none are sent, the response usually isn’t cached.
- HTTPS is cached on disk starting iOS 13 / macOS 10.15.
- You can override caching per response in the delegate methods.
- Do nothing else, the system handles the rest transparently.
Bottom line: two lines at startup give you free, fast, offline-capable networking. Don’t reinvent caching.
Category:
Tags:
Year: