Posts

Post not yet marked as solved
0 Replies
824 Views
Hi all, I have a problem in showing web view in my code. this is code that I am using  ZStack {             VStack(spacing: 0) {                                  if let url = URL(string: urlString) {                     PaymentWebView(url: url, showLoader: $showLoader) { showPaymentView,paymentUrl in                         commit(showPaymentView, paymentUrl)                     }                 }             }             .background(Color.white)             VStack{             HStack{                 Button {                     closePaymentView = false                 } label: {                                          Image("close1")                         .padding()                 }                 .padding(.top, 5)                 Spacer()                              }                 Spacer()             }             } Second is the web view that I am showing 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                         }                     }                 }             }         }     } } it is showing white screen on the initially when web view is not loaded fully
Posted Last updated
.