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

纵然是瞬间 提交于 2019-11-29 04:29:32

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

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

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

You should try this,

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

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