在iFrame里面的cordova回调 [英] cordova callback inside iFrame

查看:698
本文介绍了在iFrame里面的cordova回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cordova和nanohttpd创建了一些Android Web应用程序。主页面是使用localhost url从nanohttp加载的。主页面包含一个iFrame,它从主页面(localhost)加载来自同一域的一些页面。

I created some Android web application using cordova and nanohttpd. The main page is loaded from nanohttp with localhost url. The main page contains an iFrame which load some page from same domain as main page (localhost).

以下代码是从iFrame调用的:

The code below is called from an iFrame :

document.addEventListener("deviceready", function(){
    cordova.exec(function(response) {           
        self.doSomething(response);
    },function(error) {
        alert(JSON.stringify(error));
    },"thePlugin","showMap", param);}, false);

现在这是我的问题:来自iframe的cordova.exec能够调用Java插件并显示地图。但它无法找到javascript回调函数。是否可以在iFrame中触发回调函数?如果它可能,怎么样?

Now this is my problem : the cordova.exec from iframe is able to call Java plugin and show the map. But it is not able to find the javascript callback function. Is it possible to fire the callback function inside iFrame? If its possible, how?

推荐答案

是的,很有可能。

您的问题是由主窗口和iframe窗口之间的冲突引起的。

Your problem is caused by the conflicts between the main window and the iframe window.

请参阅此链接:
https://github.com/phonegap/phonegap/wiki/iFrame-Usage

我已经使用cordova插件联系android成功实现了这个。

I have successfully implemented this using cordova plugin contact android.

为了实现这一点,你需要这样做:

To make it possible, you need to do these:


  1. 使用 postMessage 在父窗口和子/ iframe之间创建通信。家长和孩子互相倾听。

  2. 从child / iframe发送/触发消息给父母调用cordova插件。

  3. 调用cordova插件来自父窗口

  4. 父窗口中创建全局函数以从调用> cordova代码/侧面(Java中的Java)。

  5. 找出cordova中的java文件(调用回调函数并解析结果)。 线索:callbackContext.success(参数); 。从这里开始,我们需要调用在父窗口中创建的全局函数

  6. 调用在父窗口中创建的全局函数 cordova的java文件。以下是从java代码调用js代码的片段:

  1. Create the communication between parent window and the child/iframe using postMessage. Parent and child listen each other.
  2. Send/trigger the message from child/iframe to parent to call the cordova plugin.
  3. Call the cordova plugin from parent window.
  4. Create the global function in parent window to be called from the cordova code/side (Java in android).
  5. Find out the java file in cordova that (call the callback function and parse the result back). Clue: callbackContext.success(parameter);. From here, we need to call the global function created in parent window.
  6. Call the global function created in parent window from the java file of cordova. Here is the snippet of calling js code from java code:




your_web_view .loadUrl(javascript: function_name parameter_result_from_cordova ));

your_web_view.loadUrl("javascript:function_name(parameter_result_from_cordova)");




  1. 使用postMessage将父母得到的消息发回iframe / child。

这应该工作。如果您发现任何问题,请告诉我。

This should work. Let me know if you find any issue.

这篇关于在iFrame里面的cordova回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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