【问题标题】:How to communicate with the webview and titanium如何与 webview 和钛通信
【发布时间】:2014-09-28 15:48:30
【问题描述】:

我今天从字面上开始使用钛,并怀疑我找不到答案(一定是为什么我仍然是这个领域的门外汉)。

我想在我的 Mac 上制作一个用于测试的应用程序,该应用程序在 web 视图中运行以在钛中工作,例如:

var webview = Titanium.UI.createWebView({url:'http://localhost/myfile.html'});
var window = Titanium.UI.createWindow();
window.add(webview);
window.open({modal:true});

希望他可以在 myfile.html 中访问模式钛功能,例如作为警告甚至访问麦克风。

【问题讨论】:

标签: javascript mobile titanium


【解决方案1】:

首先...如果您已经选择 Titanium 作为平台 - 通过编写使用本机代码和控件而不是 web 视图的应用程序来利用它提供的优势。正如我所看到的,Titanium 中的 webviews 与它们在本机开发中的用途相同(显示快速 html 上下文和页面,而不是在 html 中开发应用程序本身)。如果您想利用 html5+css+js 进行移动开发,请查看 PhoneGap 等。

也就是说,简要说明一下您所做的事情...您告诉您的 webview 在http://localhost 下查找文件 - 该文件不存在!您正在运行的设备上没有安装 Web 服务器。

您要做的是向您的项目添加一个 html 文件并仅引用该文件:

var webview = Titanium.UI.createWebView({url:'/myfile.html'});

或者,如果你把你的 html 放在一个文件夹中,比如说 html 目录,它看起来像这样:

var webview = Titanium.UI.createWebView({url:'/html/myfile.html'});

如果你想从 Titanium 代码中调用 webview 中的函数,你需要像这样使用evalJS()(注意 webview 中的文档必须在调用它之前完成加载):

webview.evalJS('myFunctionInsideTheWebview()');

如果你想从 webview 调用 Titanium 代码,你需要像这样添加一个全局事件监听器:

Titanium.App.addEventListener('fromwebview',function(e) { 
    Ti.API.info('here is a message from the webview : '+e.msg);
});

并像这样从 webview 调用它:

Ti.App.fireEvent(\'fromwebview\',{msg:str});

注意:全局监听器真的不推荐!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-14
    • 2019-03-09
    • 1970-01-01
    • 2020-11-17
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    相关资源
    最近更新 更多