53

Chrome mobile has recently added the ability to add to home screen, similar to iOS. This is cool but it doesn't support it as well as iOS - it doesn't support window.navigator.standalone so you can't detect whether you are running as a standalone app.

The reference says:

How can I detect if the app is running as an installed app?

You can’t, directly.

Notice it says "directly". My question is can we do it indirectly? Is there some tricky way to make an educated guess?

Community
  • 1
  • 1
mike nelson
  • 21,218
  • 14
  • 66
  • 75

9 Answers9

77

This answer comes with a huge delay, but I post it here just for other people who are struggling to find a solution.

Recently Google has implemented the CSS conditional display-mode: standalone, so there are two possible ways to detect if an app is running standalone:

Using CSS:

@media all and (display-mode: standalone) {
    /* Here goes the CSS rules that will only apply if app is running standalone */
}

Using both CSS and Javascript:

function isRunningStandalone() {
    return (window.matchMedia('(display-mode: standalone)').matches);
}
...
if (isRunningStandalone()) {
    /* This code will be executed if app is running standalone */
}

If you need more information, Google Developers has a page dedicated to this topic: https://developers.google.com/web/updates/2015/10/display-mode

josemmo
  • 6,523
  • 3
  • 35
  • 49
  • 2
    Chrome apparently had support for `display-mode: standalone` [since Nov 2014](https://code.google.com/p/chromium/issues/detail?id=289113#c23) but better late than never :) – Dan Dascalescu Jan 12 '16 at 23:11
  • weirdly this works for me only if the webapp was actually opened via the homescreen. if the user opened the page via the "Open with..." button in Chrome, it seems to not conside it standalone. any ideas why? – gaugau Mar 19 '19 at 16:10
  • 6
    For anyone who might have struggled to get this to work, remember that the manifest.json should have the entry `display: standalone` (not `display: fullscreen`) https://stackoverflow.com/a/54652992/2833684 – dhknudsen Jul 03 '19 at 08:32
51

iOS and Chrome WebApp behaves different, thats the reason i came to following:

isInWebAppiOS = (window.navigator.standalone === true);
isInWebAppChrome = (window.matchMedia('(display-mode: standalone)').matches);

Same as here: Detect if iOS is using webapp

marverix
  • 7,184
  • 6
  • 38
  • 50
Javan R.
  • 2,011
  • 1
  • 19
  • 16
14

For the IOS we have window.navigator.standalone property to check..

But for Chrome on Android, it doesn't support this property. Only way to check this is by calculating screen width and height.

Below is the code to check that:

navigator.standalone = navigator.standalone || (screen.height-document.documentElement.clientHeight<40)

I got reference from below link:

Home Screen Web Apps for Android Thanks to Chrome 31

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
murli2308
  • 2,976
  • 4
  • 26
  • 47
  • If the user starts in landscape mode you'll need some extra code like so: if ((navigator.standalone = navigator.standalone) || (screen.height-document.documentElement.clientHeight<40 || screen.width-document.documentElement.clientHeight<40)) – Christopher Grigg Jan 13 '15 at 12:19
  • 1
    40px doesn't work on newer devices with the extra UI bar at the bottom (instead of physically on the phone)... needs another ~80 or so, and I'm not sure if that UI is a consistent height device to device. – Dan Tello Jan 13 '15 at 22:45
  • 3
    There is a more reliable and standards-compliant way to do this now: [`display-mode: standalone`](http://stackoverflow.com/questions/21125337/how-to-detect-if-web-app-running-standalone-on-chrome-mobile/34516083#34516083). – Dan Dascalescu Jan 12 '16 at 23:13
5

With IOS, localstorage for the standalone and web mode are different. With android KitKat and Chrome, when you save a value in localstorage on the web version, you're able to retrieve it in standalone mode.

So, you just have to save document.documentElement.clientHeight to localstorage when user is browsing the web version (before adding to homescreen). Then, just compare the current value of document.documentElement.clientHeight with localstorage value. If the current value is >, it's standalone mode.

I tried it on several devices.

yoandm
  • 59
  • 1
  • 2
  • 1
    Interesting answer. It would be cool to see some example code. :) – mhulse May 04 '15 at 17:41
  • 2
    There is a more reliable and standards-compliant way to do this now: [`display-mode: standalone`](http://stackoverflow.com/questions/21125337/how-to-detect-if-web-app-running-standalone-on-chrome-mobile/34516083#34516083). – Dan Dascalescu Jan 12 '16 at 23:13
5

An old question but significantly better solutions available today for Chrome Android.

One of the ways(cleanest IMO). You may add Web App Manifest with a 'start_url' key with a value that adds a query string parameter to your usual homepage.

ex:- if homepage url is https://www.example.com. in Web App Manifest set

    "start_url": "https://www.example.com/?user_mode=app"

Google's guide about Web app manifest:https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/

utkarshmail2052
  • 100
  • 1
  • 5
2

For Google Chrome (Android) from version 39 onwards with web application manifest (json) and in case, it is a single page application, I use this 'trick':

In the manifest.json I put: "start_url": "standalone.html".

Normally it is (in my case index.html), but from index.html I make an identical clone: standalone.html (or whatever you fancy).

Then, to check, I use Javascript like this:

var locurl = document.location.href;
if (locurl.indexOf("standalone.html") != -1) {
    console.log("app is running standalone");
} else {
    console.log("app is running in normal browser mode");
}

That works.

It might work too in Google Chrome (mobile) version 31-38 with this meta-tag:

<meta name="application-url" content="http://my.domain.com/standalone.html">. Not tested, yet.

Klaus
  • 31
  • 5
  • 1
    I have to take this back. It's not reliable. Firefox and Google Chrome (desktop) seem to follow the start_url directive in the manifest.json as well and switch to standalone.html. Sorry. – Klaus Dec 09 '14 at 18:50
  • 1
    This trick was actually documented officially by Google under [Detecting if you are launched from the home screen](https://developers.google.com/web/updates/2014/11/Support-for-installable-web-apps-with-webapp-manifest-in-chrome-38-for-Android#news-sites). Have you tried adding the query parameter instead of renaming the file? – Dan Dascalescu Jan 12 '16 at 22:59
0

There is no 'proper' way to do it on Android, hence no API support yet. The workaround we used in my company -

  1. On first login, store screenheight in localstorage. By screenHeight i mean document.documentElement.clientHeight before page loads, since then doc grows and its not accurate measure for real available screen height.

  2. Whenever user logs in next time - check current screenheight vs stored - if it becomes bigger, the user has gone fullscreen.

There is no scenario upon which it can become smaller, IF YOU DO store FIRST TIME value for screen height.

Caveat - user that will clean cash. We chose to ignore that, since most users don't do it or do it relatively rarely, and it will suffice(in our opinion) for some time until there will be API fix for this( hopefully there will be :) )

Option b - check available screenheight vs device screen height, the matchup for a few test devices & browser modes showed some pattern( available height < 70 in webapp) - i strongly believe a wider comparison can get values that will suffice for 90% of devices in 90% cases.

All of this is a workaround, of course, i m not pretending its a solid stable way to get to desired functionality - but for us it worked, so i hope this helps somebody else who fought this bug(cant call missing such crucial api feature other way). Good luck :)enter image description here

Sergey Sob
  • 815
  • 1
  • 12
  • 27
  • 2
    There is a more reliable and standards-compliant way to do this now: [`display-mode: standalone`](http://stackoverflow.com/questions/21125337/how-to-detect-if-web-app-running-standalone-on-chrome-mobile/34516083#34516083), answered by @josemmo above. – Dan Dascalescu Jan 12 '16 at 23:14
0

To detect if it's running on MIT AI2 webview:

if (typeof window.AppInventor!="undefined") { return true; }
Nezumi
  • 11
  • 3
0

you have to work with window.navigator.standalone, this API is only works on mobile IOS safari, not event on your macbook or iMac's safari...

amdev
  • 6,703
  • 6
  • 42
  • 64