Phonegap代码作为Web应用程序

pho*_*oob 4 mobile porting web-applications cordova

我正在考虑重新使用我的phonegap html,css和js代码作为网络应用程序.我将通过并删除任何移动功能.

目的是拥有一个提供某些移动应用程序功能的Web应用程序,我目前使用的移动设备功能非常少.但我猜测维护每次发布我的移动应用程序代码都会很麻烦.

你们中的任何人以前都试过这个吗?有小费吗 ?

Thi*_*iff 7

通过响应式设计,您的phonegap代码几乎可以在任何设备上运行.了解它的运行情况(设备和操作系统)非常重要,这样您才能做出相应的响应.我预先window.deviceInfo用以下信息构建一个对象:

  • 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

我使用一个<div>名为的容器viewport来创建我的响应式容器,并根据它所使用的设备对其进行调整大小.

演示: 的jsfiddle

这是设置所有内容的初始化代码:

initializeEnvironment();
initializeDimensions();
initializePhoneGap( function () {
   //start app  
} );
Run Code Online (Sandbox Code Playgroud)

首先我成立了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' );
};
Run Code Online (Sandbox Code Playgroud)

然后我调整viewport其他需要它的东西.移动设备使用window.innerWidthwindow.innerHeight占据全屏.

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
};
Run Code Online (Sandbox Code Playgroud)

最后,我使用window.device(注意这与deviceInfo我创建的对象不同)来验证phonegap是否可用并准备就绪.deviceready当我的代码在应该运行phonegap 的设备上运行时,我会轮询该对象,而不是依赖于挑剔的事件.当initializePhoneGap()回调被调用,应用程序是准备开始.

在整个应用程序中,我包含了phonegap功能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();
    };
};
Run Code Online (Sandbox Code Playgroud)