Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect platform using Ionic 4

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.

enter image description here

Can anyone please help me how to detect platform in Ionic 4 or what can be the alternative for this?

like image 987
Darshana Avatar asked Aug 13 '18 06:08

Darshana


1 Answers

Currently Ionic 4 has support for platform detection. The following code works for me.

import { Platform } from '@ionic/angular';
...
constructor(private platform: Platform) {}
...
ngOnInit() {
   this.platform.ready().then(() => {
      if (this.platform.is('android')) {
           console.log('android');
      } else if (this.platform.is('ios')) {
           console.log('ios');
      } else {
           //fallback to browser APIs or
           console.log('The platform is not supported');
             }
      }}
like image 92
Duc Quang Avatar answered Sep 28 '22 18:09

Duc Quang