Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch Javascript Event in iOS WKWebview with Swift

I am building an app with web programming languages and want to start the camera when the user clicks on an HTML button. Since I want my camera view to be a custom one, I need to design it with Swift. So when the user clicks on this HTML button, I want to "catch" this click in Swift so I can start my native camera view.

I know it can be done with the WKWebview, but I don't really know how to do that.

For example, my Javascript (jQuery) code could look like that :

// User clicks to start the native camera with Swift $(".camera_button").click(function() {     // Function to call the camera view from JS to Swift }); 

Can you help me to do that?

Thanks.

like image 749
fraxool Avatar asked Jan 03 '16 09:01

fraxool


People also ask

How can I communicate between Swift and JavaScript?

Fortunately, I already had access to a WKWebView (from WebKit), this component natively provides the “bridge” required to communicate between Swift and Javascript . A WKWebView allows you to load a URL into a web view but also gives the user many configuration options and methods to interact with native iOS code.

How to trigger JavaScript content from Swift in iOS?

To trigger JavaScript content from Swift in iOS, I’ll use an html file with a very basic form in it and two JavaScript functions: WKWebView can be initialised with a custom configuration including its user agent and its data store for the session. It also lets you setup a controller to inject scripts or post messages to a web view.

How to capture JavaScript errors from a wkwebview?

One method to capture Javascript errors from a WKWebView uses WKScriptMessageHandler and WKUserContentController. First, create a WKScriptMessageHandler by calling the add (_:, name:) method on the WKWebView.userControllerController: let controller = webKitView?.configuration.userContentController controller?.add(self, name: "error")

What is a web view in iOS apps?

It’s common for native iOS apps to include web components, particularly to display web content. However, communicating with a web view is not always straightforward as it requires a “bridge” between Swift and Javascript.


1 Answers

Based on the answer from @Alex Pelletier, which really helped me, here is the solution the my question.

In my "loadView()" function, here is what I have :

let contentController = WKUserContentController(); contentController.addScriptMessageHandler(     self,     name: "callbackHandler" )  let config = WKWebViewConfiguration() config.userContentController = contentController  webView = WKWebView(frame: CGRectZero, configuration: config) webView.navigationDelegate = self view = webView 

My function to handle the Javascript event which is sent to Swift :

func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage)     {         if(message.name == "callbackHandler") {             print("Launch my Native Camera")         }     } 

... And finally, my Javascript (jQuery) code when a click happens on my camera button (in HTML) :

$(document).ready(function() {      function callNativeApp () {         try {             webkit.messageHandlers.callbackHandler.postMessage("camera");         } catch(err) {             console.log('The native context does not exist yet');         }     }      $(".menu-camera-icon").click(function() {         callNativeApp();     }); }); 

I hope it will help someone else :-) !

like image 54
fraxool Avatar answered Oct 14 '22 12:10

fraxool