您可以使用 PhoneGap 和 iOS 从本机代码(而不是回调中)调用 javascript 函数吗?

发布于 2024-12-09 03:53:46 字数 923 浏览 0 评论 0原文

我希望能够在我的应用程序中使用 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十六岁半 2024-12-16 03:53:46

您可以使用 UIWebView 轻松地从本机代码调用 JavaScript:

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

要在某处使用函数的结果作为 JS 函数的参数:

NSString *stringData = getStringData(); // however you get it
[webView stringByEvaluatingJavaScriptFromString:
 [NSString stringWithFormat:@"myJSFunction(%@)", stringData]];

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]];
一世旳自豪 2024-12-16 03:53:46

找到 PhoneGap 帮助程序来完成此操作...使用以下方法将 javascript 写入 webView:

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

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

    [super writeJavascript:@"alert('it works');"];
失去的东西太少 2024-12-16 03:53:46

你应该尝试这个,

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

You should try this,

[webView stringByEvaluatingJavaScriptFromString:@"sendSelectedDate()"];
舟遥客 2024-12-16 03:53:46

这对你有用吗?

http://developer.apple.com/ Library/mac/#documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/JavaScriptFromObjC.html

取自此页面:

您还可以使用参数调用 JavaScript 函数。假设您编写了一个如下所示的 JavaScript 函数:

function addImage(image, width, height) { ... }

其目的是将图像添加到网页。使用三个参数调用它: image、图像的 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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文