How to detect platform using Ionic 4

前端 未结 8 1035
慢半拍i
慢半拍i 2021-02-09 08:24

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.

<
相关标签:
8条回答
  • 2021-02-09 09:06

    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:

    public isDesktop() {
        let platforms = this.plt.platforms();
        if(platforms[0] == "core" || platforms[0] == "mobileweb") {
            return true;
        } else {
            return false;
        }
    }
    
    0 讨论(0)
  • 2021-02-09 09:07

    In case anyone is still struggling with this on ionic4/cordova, I solved it using

    import {Device} from '@ionic-native/device/ngx'
    

    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

    console.log(this.platform.platforms());
    console.log(this.device.platform);
    

    is this:

    Running on web (desktop and mobile)
    ["android", "phablet", "cordova", "mobile", "hybrid"]
    browser
    
    Running android local (apk)
    ["android", "cordova", "desktop", "hybrid"]
    Android
    

    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

    0 讨论(0)
提交回复
热议问题