您可以使用 PhoneGap 和 iOS 从本机代码(而不是回调中)调用 javascript 函数吗?
我希望能够在我的应用程序中使用 PhoneGap。我必须构建一个自定义协议/插件,以便我可以从 Javascript 调用本机方法。我知道当本机代码返回时,您可以在 Javascript 中调用成功函数。
我需要做的是从本机代码调用 javascript 函数。基本上,该应用程序将通过本地网络连接到 OSX 配套应用程序,当 OSX 应用程序将数据发送到 iOS 应用程序时,它会在 Objective C 方法中进行处理,我需要能够将结果发送到 PhoneGap/javascript 中并执行某些操作在 WebView 中使用它。
这可能吗?我只能找到有关从 javascript 调用本机的信息,而不是相反。
谢谢, 托马斯
使用下面的答案中的代码:
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];
}
给我错误“未知接收者‘theWebView’,您的意思是‘UIWebView’吗?”
更新:找到答案:使用phonegap helper我可以写这样的东西......
[super writeJavascript:@"alert('connected');"];
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');"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
UIWebView
轻松地从本机代码调用 JavaScript:要在某处使用函数的结果作为 JS 函数的参数:
You can easily call JavaScript from native code with a
UIWebView
:To use the result of a function somewhere as an arg to a JS function:
找到 PhoneGap 帮助程序来完成此操作...使用以下方法将 javascript 写入 webView:
Found the PhoneGap helper to accomplish this... Write javascript to the webView using:
你应该尝试这个,
You should try this,
这对你有用吗?
http://developer.apple.com/ Library/mac/#documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/JavaScriptFromObjC.html
取自此页面:
您还可以使用参数调用 JavaScript 函数。假设您编写了一个如下所示的 JavaScript 函数:
其目的是将图像添加到网页。使用三个参数调用它: image、图像的 URL; width,图像的屏幕宽度;高度,图像的屏幕高度。您可以通过 Objective-C 的两种方式之一调用此方法。第一个在使用 WebScriptObject 桥之前创建参数数组:
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:
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: