PhoneGap: Detect if running on desktop browser

后端 未结 30 2868
时光取名叫无心
时光取名叫无心 2020-11-29 15:47

I\'m developing a web application that uses PhoneGap:Build for a mobile version and want to have a single codebase for the \'desktop\' and mobile versions. I want to be able

30条回答
  •  悲&欢浪女
    2020-11-29 16:01

    The most trustable way we found to tell if we are in a cordova/phonegap application is to modify the cordova application's user agent using this config AppendUserAgent.

    In config.xml add:

    
    

    Then call:

    var isCordova = navigator.userAgent.match(/Cordova/i))
    

    Why?

    1. window.cordova and document.addEventListener('deviceready', function(){}); are subject to racing conditions
    2. navigator.standalone does not work when is a website (Ex: or with cordova-plugin-remote-injection)
    3. Trying to whitelist user agents to guess if it is a real browser is very complicated. Android browsers are often custom webviews.

提交回复
热议问题