How to detect browser and mobileweb platform using Ionic 4 because when I tried with below code in desktop browser it is not falling in ‘core’ condition.
if (this.platform.is('core')) {
alert('core platform');
} else {
alert('something else');
}
When I have debug in chrome developer tool it showing 'android' platform as per below snapshot.
Can anyone please help me how to detect platform in Ionic 4 or what can be the alternative for this?
Ionic 4 / Capacitor
I have written a service like so:
detect-platform.service.ts
Note: Since I use
Ionic 4/Capacitor
it givestrue
if it is ondevice
elsefalse
.The following link may help you:
https://forum.ionicframework.com/t/how-to-determine-if-browser-or-app/89149/16
or you can use the following method:
In case anyone is still struggling with this on ionic4/cordova, I solved it using
Just add it into your app.modules and run this.device.platform wherever you need to find out, to give an idea, the output on web and apk from this simple code
is this:
Now I can correctly use the plugins for browser or mobile correctly, in my case is image loading and cropping.
A full explanation can be found at https://www.damirscorner.com/blog/posts/20171124-DetectingWhereIonicAppIsRunning.html
For my use case I wanted something to distinguish between
native
andbrowser
platforms. That is, is my app running in a browser or a native mobile device. Here's the service I came up with:Currently Ionic 4 has support for platform detection. The following code works for me.
For detecting the web platform you should use Bowser . I have used this in ionic 4 for detecting browser platform i.e whether it is safari or chrome.
Step 1 : You need to install bowser in your project
Step 2 : Then you need to import it in .ts page where you want to use it. let suppose home.ts
Step 3 : Then you need to write login to check browser platform inside a function in home.ts
By calling checkBrowserPlatform() you can know the current browser name.