14

For android there is a method named addJavascriptInterface() which imports an Android JAVA object to Javascript context. Is there any equivalent of it on iOS? I have gone through some of the similar questions :
androids-addjavascriptinterface-equivalent-in-ios
calling-objective-c-function-from-javascript-in-ios-applications
But could not find a proper solution to my question .

function initiateAddCall(){
        var locus = reader.getPlace().getLocus();
        RoomHelper.postReaderLocus(JSON.stringify(locus));
}

RoomHelper is the javascript handler which am using . Is there any way i can implement this (Register a javascript handler/interface in UIWebView iOS) in UIWebView iOS .

Community
  • 1
  • 1
TomCoder09
  • 143
  • 1
  • 4

3 Answers3

8

I just used EasyJSWebView and worked pretty well. There are others solutions like WebViewJavascriptBridge but EasyJSWebView was easier to use.

RoomHelper *roomHelper = [RoomHelper new];
[self.myWebView addJavascriptInterfaces:interface WithName:@"RoomHelper"];
[self.myWebView injectJS:webView];

in javascript you call:

RoomHelper.test()

and in RoomHelper you implement a method called test

RoomHelper.h :

@interface RoomHelper : NSObject

- (void) test;

@end
Ryniere Silva
  • 443
  • 2
  • 9
  • 1
    can you please explain me how i can exactly get the same working for : function initiateAddCall(){ var locus = reader.getPlace().getLocus(); RoomHelper.postReaderLocus(JSON.stringify(locus)); } . I have check easyJSWebView but i failed to implement this in my project . RoomHelper is my handler . – TomCoder09 Oct 24 '15 at 12:48
  • Also when i used EasyJSWebView my app crashes at addjavascriptinterface: – TomCoder09 Oct 24 '15 at 12:54
  • What is the message log when it crashes? – Ryniere Silva Oct 24 '15 at 13:00
  • 1
    [UIWebView addJavascriptInterfaces:WithName:]: unrecognized selector sent to instance 0x7ff203091c70 this is the error which am getting . – TomCoder09 Oct 24 '15 at 13:05
  • 1
    Are you using Xib or Storyboard? Because you need to change the class of the UIWebview to EasyJSWebView in the viewcontroller and in the attribute inspector of Xib or Storybord – Ryniere Silva Oct 24 '15 at 13:07
  • 1
    Ok i have changed the class now . Now its working . libsystem_kernel.dylib`mach_msg_trap: but am getting this warning now . – TomCoder09 Oct 24 '15 at 13:10
  • @RyniereSilva can someone tell me how should i call the js function initiateAddCall() ?? – TomCoder09 Oct 24 '15 at 13:34
1

Had to use this for a native app project where i was using D3.JS to show the graphs. Used the below code to call the JS function

    NSString *jsString = [NSString stringWithFormat:@"showLegendGraphsFromDevice('%@',%d)",sectionName, position];
    [_webView stringByEvaluatingJavaScriptFromString:jsString];

Here

function showLegendGraphsFromDevice(name, position) 

is the JS function

Key here is creating the correct JS string with the needed quotes.

premith
  • 26
  • 1
0

You can use JavascriptCore to implement addJavascriptInterface.

NSDeveloper
  • 1,630
  • 15
  • 25