Reputation: 119
I'm literally starting today with titanium and had a doubt that I could not find the answer (must be why I am still a layman in this area).
I wanted to make an application for testing on my Mac, which functions within the web view to work in titanium, for example:
var webview = Titanium.UI.createWebView({url:'http://localhost/myfile.html'});
var window = Titanium.UI.createWindow();
window.add(webview);
window.open({modal:true});
wish within the myfile.html he could access patterns titanium functions, for example as a warning or even access to the microphone.
Upvotes: 0
Views: 747
Reputation: 13713
First of all... If you already choose Titanium as a platform - use the advantages it offers by writing applications that use native code and controls instead of a webview. As I see it, webviews in Titanium serve the same purpose as they do in native development (show quick html context and pages and not develop the app itself in html). If you'd want to leverage html5+css+js for mobile development take a look at PhoneGap and alike.
That said, a quick remark on what you've done... you told your webview to look for a file under http://localhost
- which doesn't exist! you are running on a device, which has no web server installed on it.
What you want to do is add an html file to your project and reference just that file:
var webview = Titanium.UI.createWebView({url:'/myfile.html'});
or, if you put your html's inside a folder, let's say html directory than it will look something like this:
var webview = Titanium.UI.createWebView({url:'/html/myfile.html'});
if you want to call functions inside your webview from Titanium code you need to use evalJS()
like this (pay attention that the document inside the webview has to finish loading before calling it):
webview.evalJS('myFunctionInsideTheWebview()');
If you want to call Titanium code from a webview you will need to add a global event listener like this:
Titanium.App.addEventListener('fromwebview',function(e) {
Ti.API.info('here is a message from the webview : '+e.msg);
});
and call it from a webview like this:
Ti.App.fireEvent(\'fromwebview\',{msg:str});
Pay attention: global listeners are really not recommended!
Upvotes: 2