I load HTML codes into my UIWebView via HTML strings with loadHTMLString function. There are same 1.5 MB of font files in those HTML pages. After first download of fonts, it is using them from RAM, according to cellular data usage statistics. However, when I turn off the app from task manager, launch and load the UIWebView again, those fonts are being redownloaded again.
I tried running codes below but no luck. This:
URLCache.shared.memoryCapacity = 40 * 1024 * 1024
URLCache.shared.diskCapacity = 50 * 1024 * 1024
And this:
let memoryCapacity = 40 * 1024 * 1024
let diskCapacity = 50 * 1024 * 1024
let cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: nil)
URLCache.shared = cache
So, how can I achieve disk caching for UIWebViews resources that is loading its HTML content via loadHTMLString function, not loadRequest?
You could implement a custom caching protocol that catches all web request being made by the app (including the webview) - store the content manually to disk at that point against the URL it was requested for, then when you go to load the HTML string you regex out the paths that will be loaded in that string, and swap them for local paths if there is cached content. I had to do this at my old place - not sure if there is a cleaner way to do it (note - this approach does not work if you decide to use WKWebView).