15

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

Royal Pinto
  • 235
  • 2
  • 4
  • 8

2 Answers2

38

If I understand your question correctly, you want to call an Objective C method from a javascript onClick event handler in your UIWebView. The way to do that is to redirect the browser to a URL with a custom scheme in your javascript code like this:

function buttonClicked() {
    window.location.href = "yourapp://buttonClicked";
}

Back in Objective C, declare that your view controller conforms to the UIWebViewDelegate protocol,

@interface DTWebViewController : DTViewController <UIWebViewDelegate> {
...

set your controller as the web view's delegate,

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.delegate = self;
}

and intercept the URL before it loads like this:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[request.URL scheme] isEqual:@"yourapp"]) {
        if ([[request.URL host] isEqual:@"buttonClicked"]) {
            [self callYourMethodHere];
        }
        return NO; // Tells the webView not to load the URL
    }
    else {
        return YES; // Tells the webView to go ahead and load the URL
    }
}
Ramis
  • 13,985
  • 7
  • 81
  • 100
cduhn
  • 17,818
  • 4
  • 49
  • 65
  • cool, this also works for intercepting anchor clicks, the navigationType will be UIWebViewNavigationTypeLinkClicked – BlackTigerX Mar 15 '13 at 03:05
  • function buttonClicked() { window.location.href = "yourapp://buttonClicked"; } can you tell more about these lines – ganesh manoj Dec 19 '13 at 13:40
  • 1
    This works great with the following change: yourapp://buttonClicked should contain a host component, like yourapp://www.yourapp.com/buttonClicked. Then the path will be "/buttonClicked". Otherwise, it will be an empty string. – arlomedia Jul 20 '15 at 19:16
  • 1
    This doesn't work for me on ios9, does anyone know why? – Demonedge Nov 02 '15 at 15:16
  • hi, I'm using your code with Swift, but nothing is happen, my code: unc webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest,navigationType: UIWebViewNavigationType)-> Bool{ if (request.URL!.scheme == "index//clickFunction") { self.move() return false } else return true. Thought, request is equal "index//clickFunction", it runs to else return true. So, where I was wrong, please? – AmyNguyen Apr 14 '16 at 09:11
1

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

noob
  • 545
  • 6
  • 23