Titanium Community Questions & Answer Archive

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

How to pass parameters to a Webview in 1.0?

In the pre 1.0 days we used to pass parameters was to use the App.Properties. Setting individual values using code like this:

var Props = Titanium.App.Properties;
Props.setInt('secID', e.rowData.sectionid);
Props.setString('secName', e.rowData.title);

and retrieving it in the new window using code like this:

var Props = Titanium.App.Properties;
var secID = Props.getInt('secID');
var secName = Props.getString('secName');

This works fine with 1.0 on native windows but doesn't seem to work in Webviews.

I've also seen the new webviews be pretty finicky as to what API calls they allow.

Basically I am working on a documentation app for an open source project I am involved with. I have the following structure working API Table of contents -> Sections -> functions -> function detail. The first three screen sare table views and the last screen is supposed to be the detail description page. I have my data in a SQLite db and my table views are working fine querying the db and building and displaying the tables.

I am trying to figure out how to do the last page, I basically need to pass a function id to the last page, have it query the database for the definitions stuff, and build a page. I would like to keep this as a web page cause I am going to get into multicolumn tables which would be easier to format in html.

Anyone doing something similar to this with 1.0 code?

— asked March 10th 2010 by Peter Amiri
  • 1.0
  • parameters
  • Webview
0 Comments

4 Answers

  • There's a number of ways to do this but the easiest is probably to just assign your variable(s) to your webview instance before you open it. This also gives you the benefit of giving live object references or functions to your webview context.

    You can do that like this:

    var foo = 1;
    var webview = Ti.UI.createWebView({url:'foo.js'});
    webview.foo = foo;
    webview.open();
    

    In this example, the variable foo is now available inside foo.js like this:

    alert(Ti.UI.currentWindow.foo);
    

    Any changes made to foo are reflected in the others.

    — answered March 10th 2010 by Jeff Haynie
    permalink
    2 Comments
    • At least in 1.2.2, Ti.UI.currentWindow is not available within a webview

      — commented December 13th 2010 by Rapid Development
    • I'm finding the same as RD above. Inside the webview Ti is defined as [object Object]. When I log the Ti object, I see the Ti.App and Ti.API objects. There's no UI object. Help us Jeff!!! :)

      — commented December 17th 2010 by Randy Hall
  • I just tried this and it didn't work for me. Doesn't the url of the WebView have to be an html page? That is how I had it but it didn't work. I then changed the call to a .js page but I still can't get access to the passed value.

    Here is what my starting page looks like:

    var win = Titanium.UI.currentWindow;
    
    var Props = Titanium.App.Properties;
    var secID = Props.getInt("secID");
    var secName = Props.getString('secName');
    
    var db = Titanium.Database.install('../cfwfunctions.db','cfwfunctions');
    var rows = db.execute('SELECT id, name FROM functions WHERE childfunctionsectionid = ? order by name', secID);
    
    var dataArray = []; //Start an array
    
    while (rows.isValidRow())
    {
        dataArray.push({title:rows.fieldByName('name')+'()', hasChild:true, functionid:rows.fieldByName('id')});
        rows.next();
    }
    
    // close result set and database
    rows.close();
    db.close();
    
    // create table view
    var tableViewOptions = {
            data:dataArray,
            style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
            headerTitle:secName,
        };
    
    var tableview = Titanium.UI.createTableView(tableViewOptions);
    
    tableview.addEventListener('click', function(e)
    {
        var Props = Titanium.App.Properties;
        Props.setInt('functionID', e.rowData.functionid);
        Props.setString('functionName', e.rowData.title);
    
        Ti.API.info("functionID = " + e.rowData.functionid);
        Ti.API.info("functionName = " + e.rowData.title);
    
        if (e.rowData.functionid)
        {
    
            var w = null;
            w = Titanium.UI.createWindow();
            w.orientationModes = [
                Titanium.UI.PORTRAIT,
                Titanium.UI.LANDSCAPE_LEFT,
                Titanium.UI.LANDSCAPE_RIGHT
            ];
    
            var webview = Ti.UI.createWebView({url:'detail.js'});
    
            webview.functionID = e.rowData.functionid;
            webview.functionName = e.rowData.title;    
    
            w.add(webview);
            Titanium.UI.currentTab.open(w,{animated:true});    
    
        }
    
    }
    );
    
    // add table view to the window
    Titanium.UI.currentWindow.add(tableview);
    

    and the resulting page I've tried all of the following:

    <html>
    <head>
    </head>
    <body>
        Welcome....
    
            alert(Ti.UI.currentWindow.functionName);
            alert("here");
    
    </body>
    </html>
    
    alert(Ti.UI.currentWindow.functionName);
    alert("here");
    
    — answered March 10th 2010 by Peter Amiri
    permalink
    0 Comments
  • Does anyone else have an example of a webview that uses local database data to dynamically populate the webview? I'd appreciate any sample code you could share….

    — answered March 11th 2010 by Peter Amiri
    permalink
    0 Comments
  • Hi

    I did what you did …

    But when I use it on the device, I have the following error:

    Error: Domain=NSCocoaErrorDomain Code=3000 Userinfo=0x3978b0 ''no valid aps-environment entitlement string found for application'

    Is there some other files I need modifications?

    — answered April 28th 2010 by Peter Lum
    permalink
    0 Comments
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.