Check if run on emulator, dev, production or livereload

I know there is the possibility with platform.platforms() to check on which platforms the ionic app is running.

In another post someone wrote you can use process.env.IONIC_ENV but this is not usable in angular but only on the build process right?

We need to know if we are in production mode or in developement mode and also if we are in livereload mode.

We have an api for example /api/users/current and a proxy configuration for ionic for /api.

We need to know if we have to set our apis to /api/users/current or to http:///client.bla.com/api/users/current.

How can we handle this?

Can anyone help me on this?

Which is the best way to check if we are on production or dev mode? Or if we are on browser or on mobile

For Production or Dev, you’re best to manage that setting on your own using an environment variable you can read. We use a local file in the file system that isn’t checked into source control that just says prod or dev or test and we check for the existence, that way when we publish our promotions to the build / test servers the code can know where its at.

To check if we’re on a mobile device or know, we kind of cheat. We use the push plugin (but you could use any native plugin). We look for the initialization of the push plugin, if it’s not initialized we assume we’re in the browser and not on a phone. It’s reliable in that even if the user on the phone answers ‘no’ to pushes, the plugin itself its initialized.

@bsampica thx you for your ideas, we hoped there is something more usable and integrated in the framework.

To check on which platform we are, we can check if platform.platforms() is core so we know we are on browser, and we think this should be easier to check if a cordova plugin is loaded.
To check if you are on dev or on prod we hoped there is a possibility to do this on bundling where you can set some environment variables.
process.env.IONIC_ENV is a variable but only for bundling not for ionic framework. Are there nothing like this?

I’m not sure if this is the way to resolve it:

I think, we can only check if in platform.platforms() there is no no core and no mobileweb

core -> on desktop browser with livereload
mobile AND mobileweb -> on android or ios device with livereload
mobile WITHOUT mobileweb -> on android or ios WITHOUT livereload

if there is also cordova you know you run on emulator and not on device.

So I think this should be the solution for most of I asked, it remains the question which is the best way to check if you are on production or on dev mode?

1 Like

On RC.4 there is no more mobileweb returning on platforms(), I think this is a bug and I opened a issue on github for it.
I found another solution to check if the app is running in abrowser, ass native app or as app with browser on device/emulatore so in livereload mode.

if (!document.URL.startsWith('file:///')) {
    console.log('you are on livereload mode');

    this.isLiveReload = true;
}

If the documen.URL startswith file:// I know I’m in a native app mode, if there is http or https: I know I’m running on desktop browser or on device/emulatore with livereload mode enabled.

4 Likes

checking for the URL the app loads from is a great workaround!

This is sometimes necessary as some cordova plugins don’t work well with live reload.

For example geolocation:

Note: It will not work with WKWebView WKWebView will run as http://localhost:8080

@ekhmoi that’s right the WKWebView is working so, this is giving us many other problems for CORS and ORIGIN issues!! :angry:

But anyway our solution we are doing at the moment to check if app is running in DEV is this small code

private _isDev: boolean = ((<any>window)['IonicDevServer'] != undefined);

1 Like