Can you call a javascript function from native code (not in a callback) using PhoneGap and iOS?

心不动则不痛 提交于 2019-11-27 18:24:50

问题


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');"];

回答1:


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]];



回答2:


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

    [super writeJavascript:@"alert('it works');"];



回答3:


You should try this,

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



回答4:


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];


来源:https://stackoverflow.com/questions/7719881/can-you-call-a-javascript-function-from-native-code-not-in-a-callback-using-ph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!