From 7bf365cc56b5204c31d324c6b760efad84c39f7d Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 23 May 2024 19:40:12 +0200 Subject: [PATCH] fix(ios): check if urlSchemeTask is stopped before calling its methods (#7483) --- .../Capacitor/WebViewAssetHandler.swift | 74 +++++++++++-------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/ios/Capacitor/Capacitor/WebViewAssetHandler.swift b/ios/Capacitor/Capacitor/WebViewAssetHandler.swift index 4673185d88..66121d3255 100644 --- a/ios/Capacitor/Capacitor/WebViewAssetHandler.swift +++ b/ios/Capacitor/Capacitor/WebViewAssetHandler.swift @@ -107,7 +107,7 @@ internal class WebViewAssetHandler: NSObject, WKURLSchemeHandler { } func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) { - CAPLog.print("scheme stop") + urlSchemeTask.stopped = true } func mimeTypeForExtension(pathExtension: String) -> String { @@ -155,43 +155,46 @@ internal class WebViewAssetHandler: NSObject, WKURLSchemeHandler { let urlSession = URLSession.shared let task = urlSession.dataTask(with: urlRequest) { (data, response, error) in - if let error = error { - urlSchemeTask.didFailWithError(error) - return - } + DispatchQueue.main.async { + guard !urlSchemeTask.stopped else { return } + if let error = error { + urlSchemeTask.didFailWithError(error) + return + } - if let response = response as? HTTPURLResponse { - let existingHeaders = response.allHeaderFields - var newHeaders: [AnyHashable: Any] = [:] + if let response = response as? HTTPURLResponse { + let existingHeaders = response.allHeaderFields + var newHeaders: [AnyHashable: Any] = [:] - // if using live reload, then set CORS headers - if self.isUsingLiveReload(url) { - newHeaders = [ - "Access-Control-Allow-Origin": self.serverUrl?.absoluteString ?? "", - "Access-Control-Allow-Methods": "GET, HEAD, OPTIONS, TRACE" - ] - } + // if using live reload, then set CORS headers + if self.isUsingLiveReload(url) { + newHeaders = [ + "Access-Control-Allow-Origin": self.serverUrl?.absoluteString ?? "", + "Access-Control-Allow-Methods": "GET, HEAD, OPTIONS, TRACE" + ] + } - if let mergedHeaders = existingHeaders.merging(newHeaders, uniquingKeysWith: { (_, newHeaders) in newHeaders }) as? [String: String] { + if let mergedHeaders = existingHeaders.merging(newHeaders, uniquingKeysWith: { (_, newHeaders) in newHeaders }) as? [String: String] { - if let responseUrl = response.url { - if let modifiedResponse = HTTPURLResponse( - url: responseUrl, - statusCode: response.statusCode, - httpVersion: nil, - headerFields: mergedHeaders - ) { - urlSchemeTask.didReceive(modifiedResponse) + if let responseUrl = response.url { + if let modifiedResponse = HTTPURLResponse( + url: responseUrl, + statusCode: response.statusCode, + httpVersion: nil, + headerFields: mergedHeaders + ) { + urlSchemeTask.didReceive(modifiedResponse) + } } - } - if let data = data { - urlSchemeTask.didReceive(data) + if let data = data { + urlSchemeTask.didReceive(data) + } } } + urlSchemeTask.didFinish() + return } - urlSchemeTask.didFinish() - return } task.resume() @@ -546,3 +549,16 @@ internal class WebViewAssetHandler: NSObject, WKURLSchemeHandler { "zip": "application/x-zip-compressed" ] } + +private var stoppedKey = malloc(1) + +private extension WKURLSchemeTask { + var stopped: Bool { + get { + return objc_getAssociatedObject(self, &stoppedKey) as? Bool ?? false + } + set { + objc_setAssociatedObject(self, &stoppedKey, newValue, .OBJC_ASSOCIATION_ASSIGN) + } + } +}