This is most likely a new question as I have not seen any other question related.
I am trying to connect to a cybersource payment endpoint using WKWebview on SwiftUI
I get the error
I have tried to set cookie but still won't work
WebView
public struct HTMLView: UIViewRepresentable {
var url: String
var javascriptString: String
@Binding var webViewUIModel: UIModel
public init(url: String, javascriptString: String = "", webViewUIModel: Binding<UIModel>) {
self.url = url
self.javascriptString = javascriptString
self._webViewUIModel = webViewUIModel
}
public func makeUIView(context: Context) -> WKWebView {
// let webView = WKWebView()
let webConfiguration = WKWebViewConfiguration()
let store = WKWebsiteDataStore.default()
webConfiguration.websiteDataStore = store
webConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = true
webConfiguration.defaultWebpagePreferences.allowsContentJavaScript = true
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.navigationDelegate = context.coordinator
webView.loadHTMLString(url, baseURL: nil)
return webView
}
public func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.loadHTMLString(url, baseURL: nil)
}
public func makeCoordinator() -> WebViewCordinator {
.init(javascriptString: javascriptString) {
print("HTMLView: Start")
} didHaveError: { e in
print("HTMLView: Error")
} didFinish: {
print("HTMLView: Finish")
}
}
}
Cordinator
public class WebViewCordinator: NSObject, WKNavigationDelegate, WKUIDelegate {
var javascriptString: String
var didStart: () -> Void
var didHaveError: (Error) -> Void
var didFinish: () -> Void
public init(javascriptString: String = "", didStart: @escaping () -> Void, didHaveError: @escaping (Error) -> Void, didFinish: @escaping () -> Void) {
self.javascriptString = javascriptString
self.didStart = didStart
self.didHaveError = didHaveError
self.didFinish = didFinish
}
public func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
guard let response = navigationResponse.response as? HTTPURLResponse,
let url = navigationResponse.response.url else {
decisionHandler(.cancel)
return
}
if let headerFields = response.allHeaderFields as? [String: String] {
let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url)
cookies.forEach { cookie in
print("Cookie \(cookie) \(cookie.domain)")
webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)
}
}
decisionHandler(.allow)
}
public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("Error \(error)")
didHaveError(error)
}
public func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("HTMLViewCOMMIT \( webView.url)")
}
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print( "HTMLViewFINISH \( String(describing: webView.url))")
webView.evaluateJavaScript("document.body.innerHTML") { value, error in
print("HTMLFinishValue \(value)")
print("Errot\(error)")
}
didFinish()
}
}
How can I resolve this?