你可以使用PhoneGap和iOS从本地代码(不是在回调)调用javascript函数? [英] Can you call a javascript function from native code (not in a callback) using PhoneGap and iOS?

查看:169
本文介绍了你可以使用PhoneGap和iOS从本地代码(不是在回调)调用javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够为我的应用程序使用PhoneGap。我将必须构建一个自定义协议/插件,以便我可以从Javascript调用Native方法。我知道你可以在本机代码返回时在Javascript中调用成功函数。

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.

我需要做的是从本地代码调用一个javascript函数。基本上,应用程序将通过本地网络连接到OSX伴侣应用程序,当OSX应用程序将数据发送到iOS应用程序时,它是在Objective C方法中处理,我需要能够将结果发送到PhoneGap / javascript并做某事与它在WebView中。

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.

这是可能吗?

感谢您,
Thomas

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

给我错误'未知接收者'theWebView'你的意思是'UIWebView' ?

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

更新:找到答案:使用phonegap帮助我可以这样写...

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

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


推荐答案

找到PhoneGap助手来完成这个... javascript to the webView using:

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

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

这篇关于你可以使用PhoneGap和iOS从本地代码(不是在回调)调用javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆