Skip to content

Commit

Permalink
fix(ios): check if urlSchemeTask is stopped before calling its methods (
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed May 23, 2024
1 parent 858b125 commit ab6db0a
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions ios/Capacitor/Capacitor/WebViewAssetHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit ab6db0a

Please sign in to comment.