mirror of
https://github.com/cranci1/Sora.git
synced 2026-04-21 00:22:12 +00:00
added headers support i think
Some checks failed
Build and Release IPA / Build IPA (push) Has been cancelled
Some checks failed
Build and Release IPA / Build IPA (push) Has been cancelled
This commit is contained in:
parent
c1e296ce80
commit
2bd37b50a4
1 changed files with 14 additions and 8 deletions
|
|
@ -21,13 +21,19 @@ class JSController: ObservableObject {
|
||||||
}
|
}
|
||||||
context.setObject(logFunction, forKeyedSubscript: "log" as NSString)
|
context.setObject(logFunction, forKeyedSubscript: "log" as NSString)
|
||||||
|
|
||||||
let fetchNativeFunction: @convention(block) (String, JSValue, JSValue) -> Void = { urlString, resolve, reject in
|
let fetchNativeFunction: @convention(block) (String, [String: String]?, JSValue, JSValue) -> Void = { urlString, headers, resolve, reject in
|
||||||
guard let url = URL(string: urlString) else {
|
guard let url = URL(string: urlString) else {
|
||||||
Logger.shared.log("Invalid URL",type: "Error")
|
Logger.shared.log("Invalid URL",type: "Error")
|
||||||
reject.call(withArguments: ["Invalid URL"])
|
reject.call(withArguments: ["Invalid URL"])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let task = URLSession.custom.dataTask(with: url) { data, _, error in
|
var request = URLRequest(url: url)
|
||||||
|
if let headers = headers {
|
||||||
|
for (key, value) in headers {
|
||||||
|
request.setValue(value, forHTTPHeaderField: key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let task = URLSession.custom.dataTask(with: request) { data, _, error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
Logger.shared.log("Network error in fetchNativeFunction: \(error.localizedDescription)",type: "Error")
|
Logger.shared.log("Network error in fetchNativeFunction: \(error.localizedDescription)",type: "Error")
|
||||||
reject.call(withArguments: [error.localizedDescription])
|
reject.call(withArguments: [error.localizedDescription])
|
||||||
|
|
@ -50,12 +56,12 @@ class JSController: ObservableObject {
|
||||||
context.setObject(fetchNativeFunction, forKeyedSubscript: "fetchNative" as NSString)
|
context.setObject(fetchNativeFunction, forKeyedSubscript: "fetchNative" as NSString)
|
||||||
|
|
||||||
let fetchDefinition = """
|
let fetchDefinition = """
|
||||||
function fetch(url) {
|
function fetch(url, headers) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
fetchNative(url, resolve, reject);
|
fetchNative(url, headers, resolve, reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
context.evaluateScript(fetchDefinition)
|
context.evaluateScript(fetchDefinition)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue