How To Setup And Use NativeControls In PhoneGap

As many might know the most used plugins in PhoneGap for iOS are NativeControls and ChildBrowser, but installing plugins is a bit tricky and you can’t easily find this kind of help around the internet, for example in my case I’ve learned by reading about plugins installation in PhoneGap and doing tests, so on this post I’ll cover the entire setup and usage of NativeControls (but you can use this for any other plugin in the iOS repo) in a very simple and informative way that even a PhoneGap beginner can understand. I’ll assume that you’ve already had installed and configured the Xcode environment on your Mac and is familiarized with the latest version of it. The first thing you must do is download the phonegap-plugins repo archive and extract it anywhere you like. Now go to the extracted folder and go to iPhone/NativeControls and copy the NativeControls.h and NativeControls.m to the /<Project Name>/Plugins folder on Xcode, then move the NativeControls.js to your desired place in the www folder. After all this copying and pasting you must open your PhoneGap.plist under /<Project Name>/Supporting Filesand add a new item to the Plugins array with the Key and Value NativeControls and the Type String, at the end your project should look something like this:

Project Structure

Now you’re ready to start diving into the code. The first thing you should do is include the required Javascript files into your index HTML source in this order:

<script src="phonegap-1.0.0.js" type="text/javascript" charset="utf-8"></script>
<script src="NativeControls.js" type="text/javascript" charset="utf-8"></script>

The next thing to do is go to your main Javascript file, which contains the onDeviceReady event set and put the NativeControls initialization code there. On this example we are going to use the TabBarcomponent to output something like this:

As you might have noticed I’m using the Glyphish Proicon pack there, which you can get for $25, but it’s worth every cent, since it’s such a complete icon pack for your TabBars and more. First of all you should initialize a NativeControls variable and create a assign a TabBar to it using this code:

nativeControls = window.plugins.nativeControls;
nativeControls.createTabBar();

Then you can start creating a icon/button for a tab using this JSON structure:

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

The first item is the name variable, the second is the icon label, the third is the icon path and the last one is a function that should be called every time icon is clicked. Be aware that you should insert the icon path relative to the project folder! About retina icons I really encourage you to check out this explanationon how to organize them. After you added all the icons you want to the TabBar you should show it in the screen. Then start to place the icons (the order you declare on this function they will get placed) and finally assign a TabBar to be active as the app is fired, just like this:

nativeControls.showTabBar();
nativeControls.showTabBarItems("books", "finished", "about");
nativeControls.selectTabBarItem("books");

If you want you can choose from the pre-defined TabBar icons that Apple include by default on their SDK by using these keywords as the icon item:

- tabButton:More
- tabButton:Favorites
- tabButton:Featured
- tabButton:TopRated
- tabButton:Recents
- tabButton:Contacts
- tabButton:History
- tabButton:Bookmarks
- tabButton:Search
- tabButton:Downloads
- tabButton:MostRecent
- tabButton:MostViewed

Remember that the label will be unusable since these will overwrite it, but you should put something on the label item or it won’t work. I’ve uploaded the full source code to my Gist and you can check it out at Example of NativeControls in PhoneGap – Gist. After all this hard work you’re ready to compile your application and test it. If you followed the instructions correctly everything should work. If anything goes wrong please drop us a comment and will be my pleasure to help you. Also leave a comment with your thoughts on this article or suggestions.

This entry was posted in PhoneGap, Programming and tagged , , , , , , , , , , , , , by Nathan Campos. Bookmark the permalink.

About Nathan Campos

The guy that had the idea to start this site. Mobile developer and Apple lover. I have great ideas, things to write, code or invent. Most of my time I spend programming or writing random articles, usually my opinion about the tech world (mainly mobile). I have good experience with Android, Windows Phone/Mobile, iOS, Mac OS X, Linux and Windows. I could list all the devices I own, but the list would be too big and no one will undertand why someone still owns a HP Jornada and other old school stuff.

My favorite gaming platform is iOS. I was a XBOX fan, but 3 red lights of doom happened. I think that as mobile devices are getting closer to computers, it will become the dominant gaming platform in the future.

My daily gear: Macbook Pro 17" with an i7, iPhone 4 32GB (black), HTC Titan, Galaxy S running MIUI, Galaxy Tab 7" running CyanogenMod 7 and my iPad 3G 64GB.
  • Lukacostantini

    The main.js file where is it? I create it but the NativeControls dosen’t work

    • http://twitter.com/nathanpc Nathan Campos

      You must put the main.js file under the /www folder and reference both in the index.html

      If you want send me your /www folder and I can help you out :)

      • Lukacostantini

        I added you on Facebook

  • Lukacostantini

    I put my main.js file in the www folder but dosen’t work :( , if you get your emai I send you all my www folder.

    Thanks

    • http://twitter.com/nathanpc Nathan Campos

      Send it to nathanpc [at] dreamintech [dot] net :)

      • Lukacostantini

        I send you an email with all my project folder.

        Thanks a lot!:)

  • Mauricio

    Nice! works like a charm!

  • Cool V

    Didn’t work for me. I must of did something wrong.

  • Rrafferty

    This looks great, however I have one question. Where do you point the control buttons to your pages in the www folder? I see how the icons are referenced, but how are the pages “linked”?

    • http://twitter.com/nathanpc Nathan Campos

      Sorry for that, I’ve added some new things to the article and that part was deleted, but now I’ve added it back again. (empty grey box)

      If you follow the tutorial you’ll find that it’s very simple. You add an event handler to the user click and the callback does what you want. Just add a method for showing and hiding sections. Any questions just reply. :)

  • SpaceKnight

    Be careful if you copy code from the snippets in GitHub and paste to XCode. Always use the “Raw” option at the right top corner. The double quote character breaks the Javascript code. Took me some hours to figure out what is happening.

  • http://www.facebook.com/gavinhewitt Gavin Hewitt

    I wish I found this post a few hours earlier :-)

  • Evertverhagen

    I’ve been going through your tutorial … but I’m still stuck. I’m no tech-nitwit … but you lost me after including the javascript file in the index html. There is an empty grey box that explains where to put it. Additionally … do I need to create a main.js file? I do not assume this is the case, but which file is then the main js file?

  • Jgazillo1

    Hi Nathan

    Very nice post. Question: The predefined icons from Apple, Are these the only ones that are available or are there additional ones? Where can I find a list of the ones that are available. 

    Thanks

    Joe

    • http://twitter.com/nathanpc Nathan Campos

      Sadly Apple only provides those icons in the SDK.

  • JA

    ‘PGPlugin.h’ file not found
    this issue is still not fixed, i hv googled few hours but still can’t find the solution for this

    • http://twitter.com/nathanpc Nathan Campos

      Do you have Phonegap installed?

      • JA

        thx for your prompt reply, i have fixed that issue now :)

        but now i’ve got another question, when i select a tab, e.g. Books, i want it to display another web location on the above within the app, not opening in the safari, how to do that?
        i tried:

        function books()
            {
               window.location.href=”http://www.example.com”;
            }but doesn’t seem working

        • http://twitter.com/nathanpc Nathan Campos

          Just create a div on the app and populate it with the external HTML using jQuery.

          • JA

            hi, i’ve been googling, and found that loading an external in div is not possible (due to cross domain issue), loading an iframe in app doesn’t seem working..

            do you have a practical example to show?  many thanks.

          • http://twitter.com/nathanpc Nathan Campos

            Phonegap removes that limitation and let’s you do cross-domain requests. The easiest way of doing this is by using the jQuery .load() function (examples on the link): http://api.jquery.com/load/

          • JA

            hi, Nathan, i’ve sent u an email to dreamintech coz’ it seems not appropriate to post ‘minor fix’ here.. kindly take a look, many thanks, u’re so helpful.

        • Barath

          How did you fix the “PGPlugin.h not found” issue?

    • Mike

      I have the same problem.

      ‘PGPlugin.h’ file not found

      Fresh install of xcode 4.3.1
      PhoneGap 1.6.1

      What is the solution?