How to use Form-IO in swift iOS app using Javascript in WKWebView


I want to integrate Form-IO in iOS App WkWebView. I used JavaScript to show the ui in web view. but I’m not able to see web view in screen. I tried in delegate method too but failed to load the web view.

I added webpagePreferences.allowsContentJavaScript = true to enable the Javascript still no webpage loaded in simulator.

I printed web view’s frame as well. it’s printing based on device I chose. I don’t know what wrong I’m doing here. Please correct the wrong here.

class ViewController: UIViewController, WKUIDelegate, UIScrollViewDelegate {
   
   
   var webView: WKWebView!
   
   
   override func viewDidLoad() {
       super.viewDidLoad()
       
       let config = WKWebViewConfiguration()
       
       let webpagePreferences = WKWebpagePreferences()
       webpagePreferences.allowsContentJavaScript = true
       config.defaultWebpagePreferences = webpagePreferences
       
       let contentController = WKUserContentController()
       contentController.add(self, name: "log")
       config.userContentController = contentController
       
       webView = WKWebView(frame: self.view.bounds, configuration: config)
       self.view.addSubview(webView)
       webView.frame = self.view.bounds
       webView.navigationDelegate = self
       
               
       let htmlContent = """
               
               
               
                   
                   
                   Form.io
                   
               
               
                   
                   
               
               
               """
       
       print("Before loading the HTML content")
       webView.loadHTMLString(htmlContent, baseURL: nil)
       print("After loading the HTML content")
   }

}

// WKWebView delegate method to catch console logs
extension ViewController: WKScriptMessageHandler {
   func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
       if let messageBody = message.body as? String {
           print("JavaScript log: \(messageBody)")
       }
   }
}

// WK navigation Delegate
extension ViewController: WKNavigationDelegate {
   func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
       print("delegate called....")
       let stringValue = """
                   
"""
       webView.evaluateJavaScript(stringValue)
   }
}



Discover more from TrendyShopToBuy

Subscribe to get the latest posts sent to your email.

Latest articles

spot_imgspot_img

Related articles

Leave a Reply

spot_imgspot_img

Discover more from TrendyShopToBuy

Subscribe now to keep reading and get access to the full archive.

Continue reading