WKWebView crashes after repeated reloads on iOS 18

Hi!

I have a rather complicated SwiftUI browser app with a WKWebView. There is an option to reload the website after a configurable amount of time. Starting with iOS 18, the app crashes after repeated reloads. How many reloads that are required depends on the device, sometimes 100, sometimes 1000.

Reloading is done via a timer that triggers the following code on the main thread:

let request = URLRequest(url: url, cachePolicy: policy)
self.parent.webView.load(request)

The URL is configurable and cachePolicy can be either .reloadIgnoringLocalAndRemoteCacheData or .useProtocolCachePolicy

How the crash affects the device also differs from device to device and from time to time. I have suffered from the following crashtypes:

  1. App is killed
  2. App is killed and Safari also stops working
  3. App is killed and the whole OS is really slow
  4. The WKWebView stops loading and hangs at 20%.
  5. The device is rebooted

My app has an option to disable cache. Cache is disabled by setting cachePolicy to .reloadIgnoringLocalAndRemoteCacheData and by removing all cache in a rather complicated way.

Basicly i'm doing something like this:

dataStore.removeData(ofTypes: types, modifiedSince: Date.distantPast, completionHandler: nil)
if let klazz = NSClassFromString("Web" + "History"),
           let clazz = klazz as AnyObject as? NSObjectProtocol {
            if clazz.responds(to: Selector(("optional" + "Shared" + "History"))) {
                if let webHistory = clazz.perform(Selector(("optional" + "Shared" + "History"))) {
                    let o = webHistory.takeUnretainedValue()
                    _ = o.perform(Selector(("remove" + "All" + "Items")))
                }
            }
        }
if let cachesPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first {
            let contents = (try? FileManager.default.contentsOfDirectory(atPath: cachesPath)) ?? []
            for file in contents {
                if foldersToDelete.contains(file) {
                    
                    let path = cachesPath.appending("/").appending(file)
                    
                    do {
                        try FileManager.default.removeItem(atPath: path)
                    } catch {
                        print("Can't delete cache file: \(path), error: \(error.localizedDescription)")
                    }
                    
                    
                }
            }
        }

The cache state affects the intensity of the crash. Disabling the cache shortens the time the app is working, while enabling the cache reduces the intensity of the bug.

Based on my investigation, I suspect that loading a website in a WKWebVew leaks memory in iOS 18. If the whole website needs to be requested (= caching off), it results in a more significant memory leak and a faster crash time.

Is this a known issue? Am I doing something wrong? Is there a potential workaround?

Answered by Frameworks Engineer in 812269022

Please use Feedback Assistant to file an issue, so that we get full system logs and crash logs.

Please use Feedback Assistant to file an issue, so that we get full system logs and crash logs.

FB id: FB15670666

Still no reply. My app is lacking basic functionality because of this. This bug needs attention.

WKWebView crashes after repeated reloads on iOS 18
 
 
Q