2

我无法使用 Alloy 从 WebView 与 Titanium 通信?它似乎在过去的 Ti.App.fireEvent() 中有效,但在一个新的 Alloy 项目中它不起作用。是的,我已经阅读了这些文档,但它们似乎已经过时了:https : //wiki.appcelerator.org/display/guides/Communication+Between+WebViews+and+Titanium 使用合金时没有 app.js文件 - 只有一个alloy.js 文件。如果有人有一个在合金中工作的例子,那就太好了!这是我尝试过的。

webview.html

  <html>
  <head>
    <script type="text/javascript">
    function fire(e){
      alert("Before Ti.App.fireEvent");
      Ti.App.fireEvent("fromWebview",{});
      alert("After Ti.App.fireEvent");
    }
    </script>
  </head>
  <body>
  <a href="#" onClick="fire()">Click this link to execute the fire() function</a>
  </body>
</html>

索引.xml

<Alloy>
    <Window id="w_history">
            <WebView id="webview" url="/webview.html" />
    </Window>
</Alloy>

index.js

Ti.App.addEventListener('fromWebview',function(e){
  alert("Clicked from Web");
});

$.w_history.open();

f 我在 Ti.App.fireEvent 触发之前运行代码只发出警报 - 之后的警报没有?我想这意味着 Ti.App.fireEvent 没有被执行并破坏了功能?

我整天都被困在这上面!任何帮助,将不胜感激!谢谢

4

2 回答 2

0

这似乎已经让它工作了......在 HTML 文件中,我必须在函数内定义 var Ti = window.parent.TI

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<style> html, body { margin: 5px; padding: 0px; } p {margin: 0; text-indent: 9.0pt;font-size: 13pt;line-height: 15pt;}sup{vertical-align: super;color: black;font-weight:bold;margin-right:3px;font-size: 8pt;}</style>
    <script type="text/javascript">
    function fire(e){
    var Ti = window.parent.Ti;
    alert("Before Ti.App.fireEvent");
      Ti.App.fireEvent("fromWebview",{});
      alert("After Ti.App.fireEvent");
    }
    </script>
  </head>
  <body>
  <a href="#" onClick="fire()">Click this link to execute the fire() function in the embedded script of this local page</a>
  </body>
</html>
于 2013-10-12T02:32:36.197 回答
0

我将您的代码复制到 index.js、index.xml 和 assets/webview.html 并在 Android 和 iOS 模拟器上运行。所有警报都已触发,因此您的错误必须在其他地方。

于 2013-10-12T01:16:12.310 回答