Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if ionic app is in dev serve mode(browser)

I use ionic serve to run my app on localhost.

how can I know when I'm in browser and not in android?

I tried:

navigator.platform // MacIntel navigator.platforms // undefined ionic.Platform.is('BROWSER') // false navigator.userAgent // ...iPhone... => i'm in chrome device mode 

Thank you!

like image 266
Shalev Shalit Avatar asked May 07 '15 18:05

Shalev Shalit


People also ask

How do I test my ionic app in Chrome?

Developer Options & USB Debugging are enabled by default in the Android emulator. Open the Chrome browser and navigate to the URL chrome://inspect/#devices . Your connected Android device should show up in the list of Remote Targets. On your device, open the Ionic app that you would like to debug using Chrome.

How do you serve an ionic app?

By default, ionic serve boots up a development server on localhost . To serve to your LAN, specify the --external option, which will use all network interfaces and print the external address(es) on which your app is being served. Try the --lab option to see multiple platforms at once. ionic serve uses the Angular CLI.


2 Answers

There's probably more than one way to do it, but a simple one is that cordova will only be defined on Android/iOS so you could do

if (window.cordova) {   // running on device/emulator } else {   // running in dev mode } 

Edit

Some text editors and TypeScript parsers may complain that Property 'cordova' does not exist on type 'Window'. In order to work around that, you can use the following:

if ((<any>window).cordova) {   // running on device/emulator } else {   // running in dev mode } 

By explicitly casting to type any you can avoid transpiler errors, and still accomplish what you're trying to do.

like image 106
Mirko N. Avatar answered Oct 21 '22 01:10

Mirko N.


I found I can use

ionic.Platform.platforms[0] == "browser" 

to check if the application is running in a browser or not.

Important thing, ionic.Platform.platforms is set only after $ionicPlatform.ready event is fired.

like image 20
Ygalbel Avatar answered Oct 21 '22 00:10

Ygalbel