How to detect platform using Ionic 4

How to get platform information using Ionic 4.

In Ionic 3 , we can do as mentioned below.

    if(this.platform.is('android') || this.platform.is('ios') || this.platform.is('core'))
    {
    }

But this is not working in Ionic 4. Can anyone please help me how to do this in Ionic 4 or what can be the alternate for this?

1 Like

Depending on what you want to do:

@Sujan12 How to detect desktop browser platform using this because when I tried with below it is not falling in ā€˜coreā€™ condition.

      if (this.platform.is('core')) {
        alert('core platform');
      } else {
        alert('something else');
      }

No idea, the link I posted only refers to a is('cordova').

Is there a workaround for this yet?
Detecting ā€˜coreā€™ or desktop browser in any way other than screen size?

This just got updated yesterday. Looks like ā€˜coreā€™ is supposed to work. I know it doesnā€™t now, but maybe in the next update?

1 Like

I am using

this.isApp = (!document.URL.startsWith(ā€˜httpā€™) || document.URL.startsWith(ā€˜http://localhost:8080ā€™));

So if isApp is true I am running native on Android or IOS else it is in the browser

@btsiders Great, looking forward to it. Thanks for the heads up.
@developergeme Thanks for that too. Iā€™ll try it tomorrow if there is no update.
Cheers

Desktop is now detected in 4.0.0-beta.4

1 Like

For me itā€™s still showing android platform while detecting in desktop.

Below is my ionic info :

Ionic:
ionic (Ionic CLI) : 4.1.1 (C:\Users\test\AppData\Roaming\npm\node_modules\ionic)
Ionic Framework : @ionic/angular 4.0.0-beta.1
@angular-devkit/core : 0.7.2
@angular-devkit/schematics : 0.7.2
@angular/cli : 6.1.2
@ionic/ng-toolkit : 1.0.5
@ionic/schematics-angular : 1.0.4

Cordova:

cordova (Cordova CLI) : 8.0.0
Cordova Platforms : android 6.2.3
Cordova Plugins : cordova-plugin-ionic-keyboard 2.0.5, cordova-plugin-ionic-webview 2.0.0, (and 4 other plugins)

System:

Android SDK Tools : 26.1.1 (C:\Users\test\AppData\Local\Android\sdk)
NodeJS : v8.11.3 (C:\Program Files\nodejs\node.exe)
npm : 5.6.0
OS : Windows 10

Your are still using beta.1.

Here is a post with some upgrade instructions;

Note: the current is Beta 5 but the desktop detection came in beta 4.

Thanks for upgrade instructions.

On Ionic 4 Platform is unber ā€˜@ionic/angularā€™

import { Platform } from ā€˜@ionic/angularā€™;

I did

console.log(platform.platforms());

to get the platform names when I was debugging on my browser. Note that this gives different platform names if you are testing from real android or ios device.

I also did

if(platform.is('mobileweb')){ .... }

to check if my platform was a browser.

i get the result same :-1:
if (this.platform.is(ā€˜desktopā€™)) {
alert(ā€˜desktopā€™);
} else if (this.platform.is(ā€˜iosā€™)) {
alert(ā€˜iosā€™);
} else if (this.platform.is(ā€˜androidā€™)) {
alert(ā€˜androidā€™);
// fallback to browser APIs
}
But default http://localhost:8100/home
show the result ā€˜androidā€™

I tried console.log(platform.platforms()); and tested this statement in different devices, once on browser and once on real device, it did return different platform values available.

image

1 Like

i also use second way
image
But output get on browser
image

Thatā€™s the thing. It outputs different platforms based on the device. So we canā€™t predict accurately which platform we are actually on.