Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): check if urlSchemeTask is stopped before calling its methods #7483

Merged
merged 1 commit into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
Loading