javascript event Handler in uiwebview

Viewed 13443

I am displaying one UIWebView in my iPhone application. In UIWebView I am displaying HTML page which has JavaScript also. I want to call method(of X Code) when one button is clicked in HTML page.

How can I do this. Thanks

2 Answers

Swift Version

webView.delegate = self

extension ViewController: UIWebViewDelegate {
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if request.description == "yourappname://buttonClicked" {

            // do your stuff here
            return false
        }
        return true
    }    
}

Example HTML

<a href="yourappname://buttonClicked">
    <img src="http://someurl.com/table_desktop.jpg"/ width="100%">
</a>

If you want to know how to do it in wkWebView, here is the link

Related