Phonegap NativeControls not working

Go To StackoverFlow.com

3

I've read and followed tutorials on this site or the web on how to add the NativeControls plugin to PhoneGap (Cordova 1.5) with Jquery Mobile. I haven't been able to get the toolbar to work properly.

I've tried many different things with no luck. Here is my code:

html:

    <!DOCTYPE html>
<html>
  <head>
  <!--<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />-->
  <meta name="viewport" content="width=device-width,initial-scale=1"/>
  <link rel="stylesheet" href="jquery/jquery.mobile-1.0.1.min.css"/>
  <script src="jquery/jquery-1.6.4.min.js"></script>
  <link rel="stylesheet" type="text/css" href="styles.css"/>
  <script src="jquery/jquery.mobile-1.0.1.min.js"></script>
  <title></title>  

    <!-- iPad/iPhone specific css below, add after your main css >
    <link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
    <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />-->        

    <!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
    <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>
    <script src="NativeControls.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/main.js" type="text/javascript" charset="utf-8"></script>
  </head>
    <body onload="onBodyLoad()">
      <div data-role="page" id="mainpage"> 
          <div data-role="header">
              <h1>LOGIN</h1>
          </div>
          <div data-role="content">
            <img src="images/cslogosmall2.png" style="margin-top: -10px; margin-left: -5px;"/>
            <form id="login" method="post">
              <p>
              <ul data-role="listview" data-theme="g" id="contentlist">
                  <b>Program:</b>
                  <input type="text" id="program"/>
                  <b>Username:</b>
                  <input type="text" id="username"/>
                  <b>Password:</b>
                  <input type="password"  id="password"/>
              </ul>
              </p>
              <br/>
                <button type="submit" data-theme="a" id="submit" value="Submit"></button>
            </form>
          </div>
          <!--<div data-role="footer"> 
              <h1> Main Page Footer </h1>
          </div>-->
      </div>
      <div data-role="page" id="contentpage"> 
          <div data-role="header"> 
              <h1> Content Page </h1>

          </div>
          <div data-role="content">
              <a href="#" data-role="button" onclick="history.go(-1);return false;" onClick=buttonDemo()> Back to Main Page </a>
              <a href="#" data-role="button" id="beepbtn" onClick=beepbeep()> Beep!</a>
          </div>
          <div data-role="footer"> 
              <h1> Content Page Footer </h1>
          </div>
      </div>      
  </body>
</html>

main.js:

function onBodyLoad()
{       
    $('#submit').click(function() {
        var program = document.getElementById('program').value;
        var username = document.getElementById('username').value;
        var password = document.getElementById('password').value;

        if (!program || program == "") {
            alert("Please enter a program");
            return false;
        }
        else if (!username || username == "") {
            alert("Please enter a username");
            return false;
        }
        else if (!password || password == "") {
            alert("Please enter a password");
            return false;
        }
        return true;
    });

    document.addEventListener("deviceready", onDeviceReady, false);
}

/* When this function is called, Cordova has been initialized and is ready to roll */
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
 see http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
 for more details -jm */
function onDeviceReady() {
  // Initializating TabBar
  navigator.notification.alert("Cordova is working");

  nativeControls = window.plugins.nativeControls;
  navigator.notification.alert(nativeControls);
  nativeControls.createTabBar();

  // Books tab
  nativeControls.createTabBarItem(
    "books",
    "Books",
    //"/www/tabs/book.png",
    "",
    {"onSelect": function() {
      books();
    }}
  );

  // Stats tab
  nativeControls.createTabBarItem(
    "finished",
    "Finished",
    //"/www/tabs/box.png",
    "",
    {"onSelect": function() {
      finished();
    }}
  );

  // About tab
  nativeControls.createTabBarItem(
    "about",
    "About",
    //"/www/tabs/info.png",
    "",
    {"onSelect": function() {
      about();
    }}
  );

  // Compile the TabBar
  nativeControls.showTabBar();
  nativeControls.showTabBarItems("books", "finished", "about");
  nativeControls.selectTabBarItem("books");
}


function books() {


}

function about() {

}

function finished(){

}

As you can see, I do a:

  navigator.notification.alert("Cordova is working");
  nativeControls = window.plugins.nativeControls;
  navigator.notification.alert(nativeControls);
  nativeControls.createTabBar();

to test if nativeControls variable has anything in it. I don't get any alerts. I even tried:

navigator.notification.alert(window);

with no luck. I've added the NativeControls key to Cordova.plist as NativeControls - NativeControls.

Any help would be greatly appreciated!

2012-04-05 20:50
by c1pherB1t


6

I uploaded the source code to my jQM App on the iTunes' store that has an example of the Tabbar working on both the iPhone and iPad, in both orientations. I hope it helps!!

http://zsprawl.com/iOS/2012/04/nativecontrols-plugin-for-cordovaphonegap/

2012-04-13 03:40
by zSprawl


4

I also suffered the same struggle. There might be some misunderstanding on the constructor.

I saw a post where few extra lines are added to avoid any conflict and it works!

2012-04-08 10:45
by Esteve
I'll try this out. Thanks a lot! I'll post my results and select your answer if it works - c1pherB1t 2012-04-09 03:56
After testing the code in that post, it still didn't work out but I saw somewhere code such as: Cordova.exec("NativeControls.showTabBar", options);

var onSelect = {"onSelect":onTabBarItem};
//onSelect = "hahaha";
< - c1pherB1t 2012-04-11 21:33
Cordova.exec("NativeControls.createTabBarItem", "test", "test", "tabButton:Recents", onSelect); Cordova.exec("NativeControls.createTabBarItem", "test2", "test2", "tabButton:Search", onSelect); Cordova.exec("NaviteControls.createTabBarItem", "test3", null, "tabButton:More", onSelect);

Cordova.exec("NativeControls.showTabBarItems", "test", "test2", "test3"); 
Cordova.exec("NativeControls.selectTabBarItem", "test");
< - c1pherB1t 2012-04-11 21:34
And this seems to create the tab bar, but it's not very reliable. I don't like calling an execute function for every other function either. There must be a better way - c1pherB1t 2012-04-11 21:34
Also selecting the tabbaritems don't call the onTabBarItem function which I have defined in my javascript. So, I have a tabbar without functionality. Not very useful : - c1pherB1t 2012-04-11 21:35


4

This is my code and it works:

function onDeviceReady()
{
  var nc = window.plugins.nativeControls;

  nc.createTabBar();
  nc.createTabBarItem('whoTabItem', 'Quem?', '', null);
  nc.createTabBarItem('whereTabItem', 'Onde?', '', null);
  nc.createTabBarItem('whatTabItem', 'O que?', '', null);
  nc.showTabBar();
  nc.showTabBarItems('whoTabItem', 'whereTabItem', 'whatTabItem');

}

At the very end of NativeControls.js, window.plugins.nativeControls is initialized so it should be OK:

if(!window.plugins)
  window.plugins = {};

 window.plugins.nativeControls = new NativeControls();

Your call to createTabBarItem has 5 parameters - it's only 4. Eliminate that fourth blank parameter and make a test. Maybe that's why your onSelect function is not working.

2012-04-12 04:29
by Ecil


0

You need to define nativeControls as variable at the top of main.js file, then you can use it in this line, otherwise, the js will through exception about undefined variable

 nativeControls = window.plugins.nativeControls;
2012-04-08 21:25
by Ahmad Alkhawaja
I do that in onDeviceReady function. It still doesn't work - c1pherB1t 2012-04-09 03:55
Any idea why it doesn't - c1pherB1t 2012-04-11 21:40
Ads