您可以使用 PhoneGap 和 iOS 从本机代码(不在回调中)调用 javascript 函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7719881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you call a javascript function from native code (not in a callback) using PhoneGap and iOS?
提问by tsdexter
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.
我希望能够在我的应用程序中使用 PhoneGap。我将不得不构建一个自定义协议/插件,以便我可以从 Javascript 调用本机方法。我知道当本机代码返回时,您可以在 Javascript 中调用成功函数。
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.
我需要做的是从本机代码调用 javascript 函数。基本上,该应用程序将通过本地网络连接到 OSX 配套应用程序,当 OSX 应用程序将数据发送到 iOS 应用程序时,它会以 Objective C 方法进行处理,我需要能够将结果发送到 PhoneGap/javascript 并执行某些操作在 WebView 中使用它。
Is this possible? I have only been able to find information about calling native from javascript not the other way around.
这可能吗?我只能找到有关从 javascript 调用本机的信息,而不是相反。
Thanks, Thomas
谢谢,托马斯
Using the code from Answer below here:
使用以下答案中的代码:
MyPhoneGapPlugin.m
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'?
给我错误'未知接收器'theWebView'你的意思是'UIWebView'吗?
UPDATE: Found the answer: using the phonegap helper I can write something like this...
更新:找到答案:使用 phonegap helper 我可以写这样的东西......
[super writeJavascript:@"alert('connected');"];
采纳答案by tsdexter
Found the PhoneGap helper to accomplish this... Write javascript to the webView using:
找到 PhoneGap 助手来完成此任务...使用以下命令将 javascript 写入 webView:
[super writeJavascript:@"alert('it works');"];
回答by
You can easily call JavaScript from native code with a UIWebView
:
您可以使用以下命令轻松地从本机代码调用 JavaScript UIWebView
:
[webView stringByEvaluatingJavaScriptFromString:@"myJSFunction()"];
To use the result of a function somewhere as an arg to a JS function:
要将某个函数的结果用作 JS 函数的 arg:
NSString *stringData = getStringData(); // however you get it
[webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:@"myJSFunction(%@)", stringData]];
回答by master
You should try this,
你应该试试这个
[webView stringByEvaluatingJavaScriptFromString:@"sendSelectedDate()"];
回答by Jason Sperske
Will this work for you?
这对你有用吗?
Taken from this page:
取自此页面:
You can also call JavaScript functions with arguments. Assume that you have written a JavaScript function which looks like this:
您还可以使用参数调用 JavaScript 函数。假设您编写了一个如下所示的 JavaScript 函数:
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:
其目的是将图像添加到网页中。使用三个参数调用它:图像,图像的 URL;width,图像的屏幕宽度;和高度,图像的屏幕高度。您可以通过 Objective-C 的两种方式之一调用此方法。第一个在使用 WebScriptObject 桥之前创建参数数组:
id win = [webView windowScriptObject];
NSArray *args = [NSArray arrayWithObjects:
@"sample_graphic.jpg",
[NSNumber numberWithInt:320],
[NSNumber numberWithInt:240],
nil];
[win callWebScriptMethod:@"addImage"
withArguments:args];