Last active
August 17, 2019 03:56
-
-
Save zzdjk6/f81b7ca8bfd5004818857fd81f524b72 to your computer and use it in GitHub Desktop.
WKWebView CORS Solution: ProcessResult.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let error = error { | |
let js = "window.jsCallbackBridge.resolvePromise('\(uuid)', null, '\(error.localizedDescription)')" | |
DispatchQueue.main.async { | |
self.webView?.evaluateJavaScript(js, completionHandler: nil) | |
} | |
return | |
} | |
guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else { | |
let js = "window.jsCallbackBridge.resolvePromise('\(uuid)', null, 'server error')" | |
DispatchQueue.main.async { | |
self.webView?.evaluateJavaScript(js, completionHandler: nil) | |
} | |
return | |
} | |
if let data = data, let jsonString = String(data: data, encoding: .utf8) { | |
// Use base64 string to avoid parse issue | |
let base64Str = Data(jsonString.utf8).base64EncodedString() | |
let js = "window.jsCallbackBridge.resolvePromise('\(uuid)', '\(base64Str)', null)" | |
DispatchQueue.main.async { | |
self.webView?.evaluateJavaScript(js, completionHandler: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment