Detect between a mobile browser or a PhoneGap application

后端 未结 16 1965
小蘑菇
小蘑菇 2020-11-28 03:03

Is it possible to detect if the user is accessing through the browser or application using JavaScript?

I\'m developing a hybrid application to several mobile OS thr

16条回答
  •  醉酒成梦
    2020-11-28 03:19

    Ive ben struggling with this aswell, and i know this is an old thread, but i havent seen my approach anywhere, so thought id share incase itll help someone.

    i set a custom useragent after the actual useragent :

    String useragent = settings.getUserAgentString();
    settings.setUserAgentString(useragent + ";phonegap");
    

    that just adds the phonegap string so other sites relying on detecting your mobile useragent still works.

    Then you can load phonegap like this:

    if( /phonegap/i.test(navigator.userAgent) ) 
    {
    //you are on a phonegap app, $getScript etc
    } else {
    alert("not phonegap");
    }
    

提交回复
热议问题