0
votes

here's my situation. I have a html +css + jquery well working project that I want to adapt in titanium. This project has geolocation + fb api call.

I want to adapt my project into a titanium html5 project. What I found is that I can call titanium api only through addEventListener and fireEvent functions (of course only if I use webviews).

it' my first titanium project I work with that needs geolocation and facebook api.

actually, I started to modify the previous project by adding addEventlistener into the app.js file and fireEvents into the javascript files of the previous project ( included in the first project in the html files) in the parts that need the titanium api calls (I can't call titanium api outside of app.js).

the problem is that I need some values (objects) to be returned back.

to better understand what I'm doing, here's the sequence of the events.

TITANIUM PROJECT (app.js)

var win = Ti.UI.createWindow();

var webview = Ti.UI.createWebView({
    url: 'index.html'
});

Ti.App.addEventListener('geolocation',function(){      
    //some titanium api call

    lat = x;lon=y;
    Ti.App.fireEvent('geolocation_back',{latitude:lat,longitude:lon}); 
});

win.add(webview);
win.open();

HTML + CSS + JS PROJECT (imported file into index.html, not imported into app.js)

Ti.App.fireEvent('geolocation');

var my_lat ;
var my_lon ;

Ti.App.addEventListener('geolocation_back',function(d){
    my_lat = d.latitude;
    my_lon = d.longitude;
    //do other stuff with my_lat and my_lon
});

I hope you understand what I'm doing.

my questions are:

1) is what I am doing the correct way to work with titanium and html code?

2) is there anyother way to call titanium api within html code and return variables/objects back?

EDIT this code works only on iOS and android but not on web browser. it seems that the built in server (Titanium studio or Android web browser emulator) doesn't load the Ti.* or Titanium.* objects. is there anyway to make it works on web browser?

I see the web mobile compiler creates all the titanium API in subfolders there is titanium.js and TI/* folder. can anyone explains me why the console says me Ti is not defined?

1
This is the correct way, another way would be to use webview.evalJS() to call functions or otherwise in the webview. Try yourWebView.evalJS('alert("Hello")); - Josiah Hester
the problem is that it works only on physical devices, it doesn't on web browser. I guess that I would have to do another version from scratch for web mobile browser. is that right? - Dario Rusignuolo
Wait, how are you constructing the webview? Are you using titanium to wrap a remote web site? - Josiah Hester

1 Answers

0
votes

as I said here

I found a solution!

simply add to all of your html pages the simple script below

var Ti = window.parent.Ti

have fun!