How to detect platform using Ionic 4

前端 未结 8 1033
慢半拍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: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

提交回复
热议问题