0

How does cordova trigger objective-c native method. For example- When a user taps on submit button (html button), app needs to invoke native objective c function called 'dataSubmitted'.

  • Does Cordova monitor webview navigation and based on URL tags call method internally?
  • Is there any way JavaScript can interact with Obj-c native methods except monitoring webview navigations?
  • Cordova does monitor your navigation to call native methods.Other way of communication is using WKWebview.Check answer http://stackoverflow.com/questions/37156280/how-to-capture-button-action-into-javascript-on-ios/37156704#37156704 – Sanman Jun 07 '16 at 08:50

2 Answers2

0

There are two ways to communicate with native language from JS. 1. Go to MainViewController.m -> find a function named webViewDidFinishLoad and add the follwing code snippet..

- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
    NSString *pageUrl = [theWebView.request.URL absoluteString];

    if ([pageUrl  containsString:@"xyz"]) {
      //   xyz for xyz.html
    }else if ([pageUrl  containsString:@"abc"]) {
      //  abc for abc.html
    }

    self.webView = theWebView;
    return [super webViewDidFinishLoad:theWebView];
}
  1. Develop your own native plugin with cordova.exec ;
AMAN BIRDI
  • 21
  • 4
0

There are a few libraries available to communicate between Javascript and Objective-C it seems:

WebViewJavascriptBridge: https://github.com/marcuswestin/WebViewJavascriptBridge

GAJavaScript: https://github.com/newyankeecodeshop/GAJavaScript

I suggest you to check out this beautiful SO Post which explains Javascript & Objective-C interactions in detail.

Community
  • 1
  • 1
Gandhi
  • 11,875
  • 4
  • 39
  • 63