Emerging Internet Technologies




Building Native Mobile Applications with
PhoneGap


         Becky Gibson
         Simon MacDonald



                                           © 2011 IBM Corporation
Emerging Internet Technologies




Agenda

      Mobile Application Overview
      What is PhoneGap?
      PhoneGap Architecture
      Extending PhoneGap
      Building an App
      Summary




2         Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Mobile Space




                                              http://www.abmuku.com/2011/02/24/technology/american-cell-phone-usage-infographic/


3       Building Native Mobile Applications with PhoneGap                                                 © 2011 IBM Corporation
Emerging Internet Technologies




Everyone Wants a Piece of that Pie!




4        Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Mobile Web Apps

       Fast, easy, simple
       Run in mobile browsers
       Built on W3C technologies
       Minimal access to device features




5        Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Mobile Applications

       Platform Specific
       Sold via App Stores
       Specific technologies and
        development environments
       Licensing agreements




6        Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Hybrid Applications

        Built with Web Technologies
        Access to Device Features
        Distributed via App Stores
        Some platform specific knowledge
        Licensing Agreements




7        Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Mobile App Comparison
                                         Web                         Hybrid            Native

    Dev cost                             Reasonable                  Reasonable        Expensive


    Dev time                             Short                       Short             Long


    App portability                      High                        High              None

                                                                     Native speed if
    Performance                          Fast
                                                                     needed
                                                                                       Very Fast

    Native
                                         No                          All               All
    functionality
    App store
                                         No                          Yes               Yes
    distribution

    Extensible                           No                          Yes               Yes

8                Building Native Mobile Applications with PhoneGap                                 © 2011 IBM Corporation
Emerging Internet Technologies




What’s a Developer to Do?




9       Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




What is PhoneGap?

     It’s a tool for building mobile apps using Web technologies




             HTML For Layout
             JavaScript to access device functionality
                    navigator.notification.vibrate()
             CSS for rich look and feel
             Standards Based
10            Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




How Does PhoneGap Work?




11     Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




PhoneGap Community

       Started by Nitobi in 2008
       Large, active open-source community
       350,000 downloads, 175,000 visits/month, 5000+ forum
        members
       Open Source – http://github.com/phonegap
            – Apache2 inbound (CLA)
            – MIT & BSD outbound
       Companies participating: IBM, RIM, Microsoft
       1.0 Release July, 2011




12       Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




PhoneGap Apps

     How many are there?

     You can’t tell!
        (unless you are told)



http://www.phonegap.com/apps




13             Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




PhoneGap Features
                                                 Core APIs
     –  Media (audio play/record)                              –    Accelerometer
     –  Media Capture: audio, video,                           –    Compass
        camera – (W3C)                                         –    Camera
     –  Network (W3C)                                          –    Contacts (W3C)
     –  Notification                                           –    File (W3C)
     –  Storage (HTML5 Web SQL &                               –    File Transfer
        Web Storage)
                                                               –    Geolocation (HTML5)




14         Building Native Mobile Applications with PhoneGap                          © 2011 IBM Corporation
Emerging Internet Technologies




Typical Architecture

        HTML/JS/CSS + graphic assets are on the device,
         packaged as part of the build process.
        JavaScript can store retrieved data in a SQLite
         database or from localStorage (key/value pair) for
         offline access.
        Often a server component involved.
        JavaScript communicates with the server via XHR to
         get retrieve data.
        Yes, you can load your app remotely with caveats.




15       Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies



     PhoneGap Architecture Diagram
                                                                   PhoneGap
                                                                   Application
         Web App                                                                        PhoneGap Plug-ins
          HTML                   JavaScript                                              Accelerometer       Geolocation
          CSS                    Resources                                               Camera              Media
                                                                                         Compass             Network
                                      PhoneGap




                                                                                         Contacts            Notification
                                                 JS APIs
                 HTML
                 APIs




                                                                                         File                Storage

         HTML Rendering Engine                                                           Custom Plug-ins
                                                                      PhoneGap
         (WebView)                                                    Native APIs
                            OS APIs




                                                                                                            OS APIs
                                                           Services                 Sensors
       Mobile OS
                                                           Input                    Graphics


16              Building Native Mobile Applications with PhoneGap                                                      © 2011 IBM Corporation
Emerging Internet Technologies




Writing for Multiple Devices

        Use a JavaScript Toolkit!
          Most support at least Android and iOS
              Remember that size matters!




                                             Sencha Touch




            Mobile Dojo


17       Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Best Practices
       Where possible use single HTML page
              Use JavaScript to show/hide page elements based on
               user interaction instead of linking to a separate page.
              Those JavaScript toolkits come in handy here!
       Obfuscate / crunch your JavaScript before release
       Consider offline usage
               navigator.network.connection.type
               online /offline events
               Note: Apple checks for offline support
       Utilize persistent storage
               File APIs
               Storage APIs
               SQL Lite / Lawnchair


18       Building Native Mobile Applications with PhoneGap      © 2011 IBM Corporation
Emerging Internet Technologies




Using PhoneGap


     1.  Create resources in www directory
     2.  Include phonegap.*.js in your start page (usually
         index.html)
     3.  Create onload handler
     4.  In onload handler register for deviceReady event
         from PhoneGap
     5.  When deviceready fires PhoneGap is ready.
     6.  Build, install and test for each platform




19      Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Building a PhoneGap App




                                          DEMO




20      Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Debugging?



                         Console.log is your friend!



       Wednesday @ 4:10pm – Portland 252
       Debugging Mobile Web Applications with
       weinre
       Patrick Mueller (IBM)



21     Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Extending PhoneGap
       Provide native code and JavaScript API
       – Extend Existing Features
       – Support for new sensors (NFC, ...)
       Examples of current plug-ins
       – http://github.com/phonegap/phonegap-plugins
       – Barcode scanner (Android, iOS)
       – Clipboard manager (Android, iOS)
       – Child browser (Android, iOS)
       – Contact viewer (Android)
       – iAd (iOS)
       – PayPal (iOS)

22       Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Future
       Additional Features
             Plugin Only Architecture
             W3C APIs
                 Web Notifications
                 Calendar
                 Messaging
             Crypto / security
             Menus
             More at
              http://wiki.phonegap.com/w/page/28291160/
              roadmap-planning
       We want YOU to contribute code!
             Bug fixes
             Plugins

23        Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Other PhoneGap folks presenting @ OSCON

       Wednesday @ 5:00pm - Portland 252
       Android WebKit Development - A
       Cautionary Tale
       Joe Bowser (Nitobi)

        Thursday @2:30pm – Portland Ballroom
        HTML5 and Mobile: Getting from Good
        Enough to Great
        Filip Maj (Nitobi)



24      Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation
Emerging Internet Technologies




Resources
       More Info
        –  http://www.phonegap.com/
       Getting Started
        – http://www.phonegap.com/start
       Mailing List / Google Group
        – http://groups.google.com/group/phonegap
       API Documentation
        – http://docs.phonegap.com/
       Wiki
        – http://wiki.phonegap.com/w/page/16494772/FrontPage
       CODE
        – http://github.com/phonegap

25       Building Native Mobile Applications with PhoneGap   © 2011 IBM Corporation

Building Native Mobile Applications with PhoneGap

  • 1.
    Emerging Internet Technologies BuildingNative Mobile Applications with PhoneGap Becky Gibson Simon MacDonald © 2011 IBM Corporation
  • 2.
    Emerging Internet Technologies Agenda   Mobile Application Overview   What is PhoneGap?   PhoneGap Architecture   Extending PhoneGap   Building an App   Summary 2 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 3.
    Emerging Internet Technologies MobileSpace http://www.abmuku.com/2011/02/24/technology/american-cell-phone-usage-infographic/ 3 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 4.
    Emerging Internet Technologies EveryoneWants a Piece of that Pie! 4 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 5.
    Emerging Internet Technologies MobileWeb Apps   Fast, easy, simple   Run in mobile browsers   Built on W3C technologies   Minimal access to device features 5 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 6.
    Emerging Internet Technologies MobileApplications   Platform Specific   Sold via App Stores   Specific technologies and development environments   Licensing agreements 6 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 7.
    Emerging Internet Technologies HybridApplications   Built with Web Technologies   Access to Device Features   Distributed via App Stores   Some platform specific knowledge   Licensing Agreements 7 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 8.
    Emerging Internet Technologies MobileApp Comparison Web Hybrid Native Dev cost Reasonable Reasonable Expensive Dev time Short Short Long App portability High High None Native speed if Performance Fast needed Very Fast Native No All All functionality App store No Yes Yes distribution Extensible No Yes Yes 8 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 9.
    Emerging Internet Technologies What’sa Developer to Do? 9 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 10.
    Emerging Internet Technologies Whatis PhoneGap? It’s a tool for building mobile apps using Web technologies HTML For Layout JavaScript to access device functionality navigator.notification.vibrate() CSS for rich look and feel Standards Based 10 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 11.
    Emerging Internet Technologies HowDoes PhoneGap Work? 11 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 12.
    Emerging Internet Technologies PhoneGapCommunity   Started by Nitobi in 2008   Large, active open-source community   350,000 downloads, 175,000 visits/month, 5000+ forum members   Open Source – http://github.com/phonegap – Apache2 inbound (CLA) – MIT & BSD outbound   Companies participating: IBM, RIM, Microsoft   1.0 Release July, 2011 12 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 13.
    Emerging Internet Technologies PhoneGapApps How many are there? You can’t tell! (unless you are told) http://www.phonegap.com/apps 13 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 14.
    Emerging Internet Technologies PhoneGapFeatures Core APIs –  Media (audio play/record) –  Accelerometer –  Media Capture: audio, video, –  Compass camera – (W3C) –  Camera –  Network (W3C) –  Contacts (W3C) –  Notification –  File (W3C) –  Storage (HTML5 Web SQL & –  File Transfer Web Storage) –  Geolocation (HTML5) 14 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 15.
    Emerging Internet Technologies TypicalArchitecture   HTML/JS/CSS + graphic assets are on the device, packaged as part of the build process.   JavaScript can store retrieved data in a SQLite database or from localStorage (key/value pair) for offline access.   Often a server component involved.   JavaScript communicates with the server via XHR to get retrieve data.   Yes, you can load your app remotely with caveats. 15 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 16.
    Emerging Internet Technologies PhoneGap Architecture Diagram PhoneGap Application Web App PhoneGap Plug-ins HTML JavaScript Accelerometer Geolocation CSS Resources Camera Media Compass Network PhoneGap Contacts Notification JS APIs HTML APIs File Storage HTML Rendering Engine Custom Plug-ins PhoneGap (WebView) Native APIs OS APIs OS APIs Services Sensors Mobile OS Input Graphics 16 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 17.
    Emerging Internet Technologies Writingfor Multiple Devices Use a JavaScript Toolkit! Most support at least Android and iOS Remember that size matters! Sencha Touch Mobile Dojo 17 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 18.
    Emerging Internet Technologies BestPractices   Where possible use single HTML page   Use JavaScript to show/hide page elements based on user interaction instead of linking to a separate page.   Those JavaScript toolkits come in handy here!   Obfuscate / crunch your JavaScript before release   Consider offline usage   navigator.network.connection.type   online /offline events   Note: Apple checks for offline support   Utilize persistent storage   File APIs   Storage APIs   SQL Lite / Lawnchair 18 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 19.
    Emerging Internet Technologies UsingPhoneGap 1.  Create resources in www directory 2.  Include phonegap.*.js in your start page (usually index.html) 3.  Create onload handler 4.  In onload handler register for deviceReady event from PhoneGap 5.  When deviceready fires PhoneGap is ready. 6.  Build, install and test for each platform 19 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 20.
    Emerging Internet Technologies Buildinga PhoneGap App DEMO 20 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 21.
    Emerging Internet Technologies Debugging? Console.log is your friend! Wednesday @ 4:10pm – Portland 252 Debugging Mobile Web Applications with weinre Patrick Mueller (IBM) 21 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 22.
    Emerging Internet Technologies ExtendingPhoneGap   Provide native code and JavaScript API – Extend Existing Features – Support for new sensors (NFC, ...)   Examples of current plug-ins – http://github.com/phonegap/phonegap-plugins – Barcode scanner (Android, iOS) – Clipboard manager (Android, iOS) – Child browser (Android, iOS) – Contact viewer (Android) – iAd (iOS) – PayPal (iOS) 22 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 23.
    Emerging Internet Technologies Future   Additional Features   Plugin Only Architecture   W3C APIs   Web Notifications   Calendar   Messaging   Crypto / security   Menus   More at http://wiki.phonegap.com/w/page/28291160/ roadmap-planning   We want YOU to contribute code!   Bug fixes   Plugins 23 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 24.
    Emerging Internet Technologies OtherPhoneGap folks presenting @ OSCON Wednesday @ 5:00pm - Portland 252 Android WebKit Development - A Cautionary Tale Joe Bowser (Nitobi) Thursday @2:30pm – Portland Ballroom HTML5 and Mobile: Getting from Good Enough to Great Filip Maj (Nitobi) 24 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation
  • 25.
    Emerging Internet Technologies Resources   More Info –  http://www.phonegap.com/   Getting Started – http://www.phonegap.com/start   Mailing List / Google Group – http://groups.google.com/group/phonegap   API Documentation – http://docs.phonegap.com/   Wiki – http://wiki.phonegap.com/w/page/16494772/FrontPage   CODE – http://github.com/phonegap 25 Building Native Mobile Applications with PhoneGap © 2011 IBM Corporation