webView usage guideline
hello,
i'm trying to figure out possible use of a (android) webview. i've read through the various Q&A, but it seems there's some confusion, therefore i ask a dedicated more general question. particularly i'm interested in creating a webview from a remote url (a html/js page on a webserver), and if and how it's possible to trigger an event within the remote html/js page and catch it locally. i'm not talking about downloading and parsing/eval()uating, rather about the interaction with a javascript on a webserver by using custom events, e.g. Ti.App.fireEvent in the remote html/js page and a corresponding event listener in the webview created from the given url. basically that would mean to add webbrowser functionality within a webview. in native android apps you can enable this behaviour explicitely by enabling the addJavascriptInterface() method, my questions is whether or not this is the intended use with TI.
thanks for clarification,
uno
5 Answers
-
Accepted Answer
In general, the web view's purpose is to display web content (whether static or dynamic), and if you need to use Titanium APIs, you pass events between the WebView itself and your application. We only expose a small subset of the Titanium API to the WebView, namely:
Ti.App.addEventListener
Ti.App.removeEventListener
Ti.App.fireEvent
Ti.API.info / log / debug / warnWe don't plan on allowing remote URLs to have access to these APIs directly (since that could be a security hole), but it would be possible to use an iframe and forward the "Titanium" object into it.
-
I've found the solution and it was really easy.
Instead of loading the remote web page in a webview create a local html file and load your remote page inside an iframe, forward the ti.API and Ti.App objects to it and you can trigger events listened in Titanium easily from inside you remote webpage, just remember to call fireEvent against the correct object nameTiApp.fireEvent
<html> <head> <script> function init() { // Forward the Ti.API and Ti.app objects to the iFrame to allow callbacks window.myframe.TiAPI = Ti.API; window.myframe.TiApp = Ti.App; } </script> </head> <body> <iframe name="myframe" src="http://mysite/mypage.html" width="100%" height="100%" onload="init()"> </iframe> </body> </html>
-
hello Marshall, thanks. that makes it much clearer to me.
-
Hi Marshall, could you provide and example of how "it would be possible to use an iframe and forward the "Titanium" object into it"?
-
Hi Marshall, I would also like to know how to do this - if you could forward me an example too that would be very much appreciated.
Many thanks