Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

webView usage guideline

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,

— asked March 29th 2010 by u no
  • android
  • fireevent
  • mobile
  • webview

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.API.info / log / debug / warn

    We 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.

    — answered March 29th 2010 by Marshall Culpepper
    1 Comment
    • Is it still true with 1.7.1 that Ti.API.log is exposed to the WebView? When I try this, I get an error "Error calling method on NPObject!.

      — commented July 25th 2011 by Jose Amanaceo
  • 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

                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;
                <iframe name="myframe" src="http://mysite/mypage.html" width="100%" height="100%" onload="init()">
    — answered November 25th 2011 by Nicola Gervasi
  • hello Marshall, thanks. that makes it much clearer to me.

    — answered March 29th 2010 by u no
  • Hi Marshall, could you provide and example of how "it would be possible to use an iframe and forward the "Titanium" object into it"?

    — answered November 24th 2011 by Nicola Gervasi
  • 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

    — answered August 30th 2012 by Victor Leitch
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.