7

I'm hoping to be able to use PhoneGap for my app. I will have to build a custom protocol/plugin so that I can call Native methods from the Javascript. I know you can call a success function in the Javascript when the native code returns.

What I need to be able to do is call a javascript function from the native code. Basically the app will connect to an OSX companion app over local network and when the OSX app send data to the iOS app it is processed in an Objective C method, I need to be able to send the result into the PhoneGap/javascript and do something with it in the WebView.

Is this possible? I have only been able to find information about calling native from javascript not the other way around.

Thanks, Thomas

Using the code from Answer below here:

MyPhoneGapPlugin.m

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
    NSLog(@"Connected To %@:%i.", host, port);

    NSString* jsString = [NSString stringWithFormat:@"alert(connected to: %@);", host];
    [theWebView stringByEvaluatingJavaScriptFromString:jsString];

    [self readWithTag:2];
}

Giving me the error 'Unknown receiver 'theWebView' did you mean 'UIWebView'?

UPDATE: Found the answer: using the phonegap helper I can write something like this...

    [super writeJavascript:@"alert('connected');"];
tsdexter
  • 2,911
  • 4
  • 36
  • 59

4 Answers4

10

You can easily call JavaScript from native code with a UIWebView:

[webView stringByEvaluatingJavaScriptFromString:@"myJSFunction()"];

To use the result of a function somewhere as an arg to a JS function:

NSString *stringData = getStringData(); // however you get it
[webView stringByEvaluatingJavaScriptFromString:
 [NSString stringWithFormat:@"myJSFunction(%@)", stringData]];
  • Awesome this should work for me. I didn't even think to use the UIWebView class, I was too focused on the PhoneGap API I guess. Thanks so much! – tsdexter Oct 10 '11 at 23:49
  • Thanks for the update. That probably would have been my next question. – tsdexter Oct 10 '11 at 23:53
  • Can this only be used in certain places? I'm trying to use it in a PhoneGap plugin file 'MyPhoneGapPlugin.m' inside an AsyncSockets didConnectToHost so I can alert to the webview that it connected successfully and display the IP but it it throwing an error, please see question for updated code and error. – tsdexter Oct 12 '11 at 18:49
  • Hmm. Well, that would mean that `theWebView` isn't visible from inside that function. You need to be able to see an `@property UIWebView *theWebView` or `UIWebView *theWebView` from `MyPhoneGapPlugin.m`. You've probably already tried to ensure that, but.. apart from that I don't really know off the top of my head why it wouldn't be working. –  Oct 13 '11 at 02:59
  • Thanks, I'll report back results, but I've likely found a better way to solve this particular problem. Although, I'll likely still needs this method for other tasks on the project. – tsdexter Oct 13 '11 at 23:22
  • Found the best/appropriate way to do it. PhoneGap has a writeJavascript helper... See updated question. Thanks for the help. – tsdexter Oct 24 '11 at 23:07
  • 2
    Cut n pasters , Watch the case sensitivity: stringByEvaluatingJavaScriptFromString NOT stringByEvaluatingJavascriptFromString – dijipiji Oct 05 '12 at 08:47
  • @Lemonsanver Updated the function name. Sorry, was off the top of my head at the time. –  Oct 06 '12 at 01:29
  • @JoshKethepalli Please do not ask questions like this, in comments below other questions. If you are having trouble with this ask a separate question. –  Mar 18 '15 at 09:06
4

Found the PhoneGap helper to accomplish this... Write javascript to the webView using:

    [super writeJavascript:@"alert('it works');"];
tsdexter
  • 2,911
  • 4
  • 36
  • 59
  • Can you please elaborate your answer. I am having the same issue and am new to Cordova. I called it from the MainViewController and also from my Phonegap plugin, it did not responded in the webview. Is their something I am missing? – bhuwansahni Jul 17 '13 at 13:30
  • @bhuwansahni I'm afraid cordova may have changed a lot since I did this so I'm not quite sure what would be required now. I was able to call it just like that from any methods in my app - maybe check out this article on the docs for more info: http://docs.phonegap.com/en/2.0.0/guide_plugin-development_ios_index.md.html – tsdexter Jul 18 '13 at 15:13
2

You should try this,

[webView stringByEvaluatingJavaScriptFromString:@"sendSelectedDate()"];
Hemang
  • 26,840
  • 19
  • 119
  • 186
master
  • 21
  • 2
0

Will this work for you?

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/JavaScriptFromObjC.html

Taken from this page:

You can also call JavaScript functions with arguments. Assume that you have written a JavaScript function which looks like this:

function addImage(image, width, height) { ... }

Its purpose is to add an image to a web page. It is called with three arguments: image, the URL of the image; width, the screen width of the image; and height, the screen height of the image. You can call this method one of two ways from Objective-C. The first creates the array of arguments prior to using the WebScriptObject bridge:

id win = [webView windowScriptObject];



NSArray *args = [NSArray arrayWithObjects:
                 @"sample_graphic.jpg",
                 [NSNumber numberWithInt:320],
                 [NSNumber numberWithInt:240],
                  nil];

[win callWebScriptMethod:@"addImage"
            withArguments:args];
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124