Search code examples
javascriptwebviewtitanium

Call Titanium function from JS in Webview


I have titanium mobile app. In this app i have a webview, in which i have local html file. In this html file i call JS function from a local js file. In this js file i would like to call titanium function like:

var fileName = 'test.json'; 
    var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, fileName);    
    alert(file);

But alert is not going, i suppose because it doesn't know Ti function inside JS.

What is the best way for such cases?

All my app is based on the webviews and i use small JS tricks inside html, but it's necessary for me to contact with Titanium also and it's annoying to make it all through fire\listen events as in appcelerator docs...


Solution

  • In webView, the code is execude like in a browser, it's not "compiled" by Titanium (it remains an HTML file). What you only can do is use Ti.App events. If you are carefull managing the Ti.App event (http://docs.appcelerator.com/titanium/latest/#!/guide/Event_Handling-section-29004896_EventHandling-Application-LevelEvents) you can do whatever you want inside a web view, just define a extra couple of eventListener...