Hi all, I am getting an issue in calling a web view through use of WKWebView in my app. It is showing white blank screen up to the point web is not loaded. I want to hide that white screen. Don't know how?
struct PaymentWebView: UIViewRepresentable {
var webView = WKWebView()
var url: URL
// Viewmodel object
@Binding var showLoader:Bool
// @Binding var showPaymentView:Bool
// @Binding var paymentSuccessUrl:String
var commit: (Bool, String) -> () = {, in }
// Make a coordinator to co-ordinate with WKWebView's default delegate functions
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> WKWebView {
// Enable javascript in WKWebView
context.coordinator.addProgressObserver()
self.webView.clipsToBounds = true
webView.navigationDelegate = context.coordinator
debugPrint("makeUIView:(url)")
webView.load(URLRequest(url: url))
return webView
}
func updateUIView(_ webView: WKWebView, context: Context) {
}
class Coordinator : NSObject, WKNavigationDelegate {
var parent: PaymentWebView
var valueSubscriber: AnyCancellable? = nil
var webViewNavigationSubscriber: AnyCancellable? = nil
init(_ uiWebView: PaymentWebView) {
self.parent = uiWebView
}
deinit {
valueSubscriber?.cancel()
webViewNavigationSubscriber?.cancel()
}
func addProgressObserver() {
parent.webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
parent.webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let key = change?[NSKeyValueChangeKey.newKey] {
if String(describing:key).contains("payment/success") {
// parent.paymentSuccessUrl = String(describing:key)
parent.commit(false, String(describing:key))
// parent.showPaymentView = false
}
debugPrint("observeValue (String(describing:key))")
}
if let o = object as? WKWebView, o == parent.webView {
if keyPath == #keyPath(WKWebView.isLoading) {
if parent.webView.isLoading {
DispatchQueue.main.async {
self.parent.showLoader = true
}
} else {
DispatchQueue.main.async {
self.parent.showLoader = false
}
}
}
}
}
}
}