Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap code as a web app

I am thinking of re-using my phonegap html, css and js code as a web app. I would be going through and removing any mobile only functionalities.

The purpose is to have a web app which offers some of the mobile apps functionality, I use very little mobile device features currently. But I am guessing maintaining with each release of my mobile app code will be troublesome.

Any of you guys tried this before ? Any tips ?

like image 971
phonegapnoob Avatar asked Feb 17 '23 09:02

phonegapnoob


1 Answers

With a responsive design your phonegap code should run on almost any device. It's important to know what it is running on (both device and OS) so you can respond accordingly. I build a window.deviceInfo object up front with the following information:

  • window.deviceInfo.type: handheld, tablet, desktop
  • window.deviceInfo.brand: ios, android, microsoft, webos, blackberry
  • window.deviceInfo.mode: browser, standalone, webview
  • window.deviceInfo.mobile: true, false 
  • window.deviceInfo.phonegap: true, false

I use a single container <div> called viewport to create my responsive container and size it based on the device it's on.

Demo: jsFiddle

This is the initialization code to set everything up:

initializeEnvironment();
initializeDimensions();
initializePhoneGap( function () {
   //start app  
} );

First I set up window.deviceInfo.

function initializeEnvironment() {
    //window.deviceInfo.type: handheld, tablet, desktop
    //window.deviceInfo.brand: ios, android, microsoft, webos, blackberry
    //window.deviceInfo.mode: browser, standalone, webview
    //window.deviceInfo.mobile: true, false 
    //window.deviceInfo.phonegap: true, false 

    var userAgent = window.navigator.userAgent.toLowerCase();
    window.deviceInfo = {};

    if ( /ipad/.test( userAgent ) || ( /android/.test( userAgent ) && !/mobile/.test( userAgent ) ) ) {
        window.deviceInfo.type = 'tablet';
    } else if ( /iphone|ipod|webos|blackberry|android/.test( userAgent ) ) {
        window.deviceInfo.type = 'handheld';
    } else {
        window.deviceInfo.type = 'desktop';
    };

    if ( /iphone|ipod|ipad/.test( userAgent ) ) {
        var safari = /safari/.test( userAgent );
        window.deviceInfo.brand = 'ios';
        if ( window.navigator.standalone ) {
            window.deviceInfo.mode = 'standalone';
        } else if ( safari ) {
            window.deviceInfo.mode = 'browser';
        } else if ( !safari ) {
            window.deviceInfo.mode = 'webview';
        };
    } else if ( /android/.test( userAgent ) ) {
        window.deviceInfo.brand = 'android';
        window.deviceInfo.mode = 'browser';
    } else if ( /webos/.test( userAgent ) ) {
        window.deviceInfo.brand = 'webos';
        window.deviceInfo.mode = 'browser';
    } else if ( /blackberry/.test( userAgent ) ) {
        window.deviceInfo.brand = 'blackberry';
        window.deviceInfo.mode = 'browser';
    } else {
        window.deviceInfo.brand = 'unknown';
        window.deviceInfo.mode = 'browser';
    };
    window.deviceInfo.mobile = ( window.deviceInfo.type == 'handheld' || window.deviceInfo.type == 'tablet' );
};

Then I resize the viewport and anything else that needs it. Mobile devices use window.innerWidth and window.innerHeight to take up the full screen.

function initializeDimensions() {
    var viewport = document.getElementById( 'viewport' );
    if ( window.deviceInfo.mobile ) {
        viewport.style.width = window.innerWidth + 'px';
        viewport.style.height = window.innerHeight + 'px';
    } else {
        //requirements for your desktop layout may be different than full screen
        viewport.style.width = '300px';
        viewport.style.height = '300px';
    };
    //set individual ui element sizes here
};

Finally, I use window.device (note this is not the same as the deviceInfo object I create) to verify if phonegap is available and ready. Instead of relying on the finicky deviceready event, I poll that object when my code is running on a device that should be running phonegap. When the initializePhoneGap() callback is called, the app is ready to start.

Throughout the app, I wrap phonegap features in if( window.deviceInfo.phonegap ) {}.

function initializePhoneGap( complete ) {
    if ( window.deviceInfo.brand == 'ios' && window.deviceInfo.mode != 'webview' ) {
        window.deviceInfo.phonegap = false;
        complete();
    } else if ( window.deviceInfo.mobile ) {
        var timer = window.setInterval( function () {
            if ( window.device ) {
                window.deviceInfo.phonegap = true;
                complete();
            };
        }, 100 );
        window.setTimeout( function () { //failsafe
            if ( !window.device ) { //in webview, not in phonegap or phonegap failed
                window.clearInterval( timer );
                window.deviceInfo.phonegap = false;
                complete();
            };
        }, 5000 ); //fail after 5 seconds
    } else {
        window.deviceInfo.phonegap = false;
        complete();
    };
};
like image 160
ThinkingStiff Avatar answered Feb 20 '23 09:02

ThinkingStiff