fix(ios): fix ios http and ws (#9805)

This commit is contained in:
EYHN
2025-01-21 00:16:21 +00:00
parent 69e4a5e9b8
commit 97207a7ad5
3 changed files with 26 additions and 13 deletions

View File

@@ -53,8 +53,8 @@ class AffineHttpHandler: NSObject, WKURLSchemeHandler {
request.setValue(value, forHTTPHeaderField: key)
}
URLSession.shared.dataTask(with: request) {
rawData, rawResponse, error in
let task = URLSession.shared.dataTask(with: request) {
(rawData, rawResponse, error) in
urlSchemeTask.stopped?.withLock({
if $0 {
return
@@ -91,12 +91,16 @@ class AffineHttpHandler: NSObject, WKURLSchemeHandler {
}
})
}
task.resume()
urlSchemeTask.dataTask = task
}
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
urlSchemeTask.stopped?.withLock({
$0 = true
})
urlSchemeTask.dataTask?.cancel()
}
}
@@ -106,9 +110,18 @@ private extension WKURLSchemeTask {
return objc_getAssociatedObject(self, &stoppedKey) as? Mutex<Bool> ?? nil
}
set {
objc_setAssociatedObject(self, &stoppedKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
objc_setAssociatedObject(self, &stoppedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
var dataTask: URLSessionDataTask? {
get {
return objc_getAssociatedObject(self, &dataTaskKey) as? URLSessionDataTask
}
set {
objc_setAssociatedObject(self, &dataTaskKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}
private var stoppedKey = malloc(1)
private var dataTaskKey = malloc(1)

View File

@@ -168,7 +168,7 @@ class AffineWsHandler: NSObject, WKURLSchemeHandler {
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
urlSchemeTask.stopped?.withLock({
$0 = false
$0 = true
})
urlSchemeTask.wsTask?.cancel(with: .abnormalClosure, reason: "Closed".data(using: .utf8))
}
@@ -180,7 +180,7 @@ private extension WKURLSchemeTask {
return objc_getAssociatedObject(self, &stoppedKey) as? Mutex<Bool> ?? nil
}
set {
objc_setAssociatedObject(self, &stoppedKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
objc_setAssociatedObject(self, &stoppedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
var wsTask: URLSessionWebSocketTask? {
@@ -188,7 +188,7 @@ private extension WKURLSchemeTask {
return objc_getAssociatedObject(self, &wsTaskKey) as? URLSessionWebSocketTask
}
set {
objc_setAssociatedObject(self, &stoppedKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
objc_setAssociatedObject(self, &wsTaskKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}

View File

@@ -14,25 +14,25 @@ import '@affine/core/bootstrap/browser';
*/
const rawFetch = globalThis.fetch;
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
const url = new URL(
let url = new URL(
typeof input === 'string'
? input
: input instanceof URL
? input.toString()
: input.url,
globalThis.location.origin
);
).href;
if (url.protocol === 'capacitor:') {
if (url.startsWith('capacitor:')) {
return rawFetch(input, init);
}
if (url.protocol === 'http:') {
url.protocol = 'affine-http:';
if (url.startsWith('http:')) {
url = 'affine-http:' + url.slice(5);
}
if (url.protocol === 'https:') {
url.protocol = 'affine-https:';
if (url.startsWith('https:')) {
url = 'affine-https:' + url.slice(6);
}
return rawFetch(url, input instanceof Request ? input : init);