Skip to content

Instantly share code, notes, and snippets.

@zzdjk6
Last active August 17, 2019 03:56
Show Gist options
  • Save zzdjk6/f81b7ca8bfd5004818857fd81f524b72 to your computer and use it in GitHub Desktop.
Save zzdjk6/f81b7ca8bfd5004818857fd81f524b72 to your computer and use it in GitHub Desktop.
WKWebView CORS Solution: ProcessResult.swift
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