WKWebView shows old version of site

Hello,

I've got a problem with the WKWebView occurring only sometimes. The problem is that when loading a remote URL the webview sometimes just shows an old version of the site that it has shown before. When reloading the page in Safari, Safari shows the newest version of the page. Even after removing the webview and initializing a new one, the old version of the site gets shown. Here's how I init and load the webview:

        self.webview = WKWebView.init(frame: rc);
        
        self.webview!.navigationDelegate = self;
        self.webview!.scrollView.delegate = self;
        self.webview!.isHidden = hidden != nil && hidden!.boolValue;
        self.webview!.scrollView.showsHorizontalScrollIndicator = scrollBar == nil || !scrollBar!.boolValue;
        self.webview!.scrollView.showsVerticalScrollIndicator = scrollBar == nil || !scrollBar!.boolValue;

        self.viewController.view.addSubview(self.webview!);


                // Clearing the cache
                URLCache.shared.removeAllCachedResponses();

                if #available(iOS 9.0, *) {
                    let dataTypes: Set<String> = [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache]
                        WKWebsiteDataStore.default().removeData(ofTypes: dataTypes, modifiedSince: Date.init(timeIntervalSince1970: 0
                    )) {
                    };
                }

                let urlInstance: URL? = URL(string: url);
                
                if(urlInstance == nil) {
                    return;
                }
                
                let request: NSMutableURLRequest = NSMutableURLRequest.init(url: urlInstance!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60.0);
                let headers: NSDictionary? = arguments["headers"] as? NSDictionary;
                
                if(headers != nil) {
                    for(key,value) in headers! {
                        request.setValue((value as! String), forHTTPHeaderField: key as! String);
                    }
                }
                
                self.webview?.load(request as URLRequest);


How can I fix this problem? It happens randomly, so debugging it might be hard.


Best regards,

Jan

Replies

The the headers that your server is returning and your URL request cache policy. Perhaps either you aren't publishing updated timestamps and/or your web view is caching all requests.