Using the same code base for phone calls and the Internet?

Is it possible to use the same code base for our mobile web application and telephone application? They should be similar, the phone version just allows us to do more. Can we determine if it doesn’t work on phone calls and sequencer calls in the api phone or does it make sense to separate them.

+5
source share
3 answers

Of course, you can use most of the same code base. Some call APIs are the same in html5 (e.g. localStorage), so there are no differences in code.

If you use the phonegap build service, it will add the phonegap.js / cordova.js script file to your project root. Just include it in your html all the time. Then you can determine if your application is running in a telephone congestion:

var isPhonegap = function() {
  return (typeof(cordova) !== 'undefined' || typeof(phonegap) !== 'undefined');
}

if (isPhonegap()) {
  // phonegap.js/cordova.js exists.. now let handle the onDeviceReady event
} else {
  // in-browser
}

If you need a regular startup code, put it in a function and call this function from the onDeviceReady handler and the else block above.

If the phone delay you are calling api does not have an exact name, like html5 (for example, for the Moz * or WebKit * prefix), just wrap both inside the new name. For instance:

var requestFileSystem = (isPhonegap() ? requestFileSystem : window.WebKitRequestFileSystem);

If the API used by your phone does not have the equivalent of html5, try to duplicate the functionality yourself in javascript, if possible, otherwise you will simply lose functionality in your browser. But make sure it is graceful enough without this feature.

. , , .., Ripple Chrome.

+17

, -...

deviceready , : ", , ".

  • , , iOS, MainViewController.m viewDidLoad, javascript, -, , , , (, )

    MainViewController.m:

    - (void) viewDidLoad
    {
        [super viewDidLoad];
        NSString* jsString = [NSString stringWithFormat:@"isAppNative = true;"];
        [self.webView stringByEvaluatingJavaScriptFromString:jsString];
    }
    
  • index.html :

    function onBodyLoad()
    {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    
    function onDeviceReady(){;
        myApp.run();
    }
    
    try{
        if(isAppNative!=undefined);
    }catch(err){
        $(document).ready(function(){
            myApp.run();
        });
    }
    
+2

. PhoneGap - . . . /, .

(. ): http://bennolan.com/2011/08/22/phonegap-detection.html. - if (window.PhoneGap){...}

0

All Articles