How to detect platform using Ionic 4

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

    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

    npm install bowser@2.7.0 --save-exact
    

    Step 2 : Then you need to import it in .ts page where you want to use it. let suppose home.ts

    import Bowser from "bowser";
    

    Step 3 : Then you need to write login to check browser platform inside a function in home.ts

    checkBrowserPlatform() {
      const browser = Bowser.getParser(window.navigator.userAgent);
      const browserName = browser.getBrowserName();
        console.log(browserName);
      }
    

    By calling checkBrowserPlatform() you can know the current browser name.

    0 讨论(0)
  • 2021-02-09 08:45

    For my use case I wanted something to distinguish between native and browser platforms. That is, is my app running in a browser or a native mobile device. Here's the service I came up with:

    import { Injectable } from '@angular/core';
    import {Platform} from '@ionic/angular';
    
    
    type CurrentPlatform = 'browser' | 'native';
    
    @Injectable({
      providedIn: 'root'
    })
    export class CurrentPlatformService {
    
      private _currentPlatform: CurrentPlatform;
    
      constructor(private platform: Platform) {
        this.setCurrentPlatform();
      }
    
      get currentPlatform() {
        return this._currentPlatform;
      }
    
      isNative() {
        return this._currentPlatform === 'native';
      }
      isBrowser() {
        return this._currentPlatform === 'browser';
      }
    
      private setCurrentPlatform() {
        // Are we on mobile platform? Yes if platform is ios or android, but not desktop or mobileweb, no otherwise
        if (
            this.platform.is('ios')
            || this.platform.is('android')
            && !( this.platform.is('desktop') || this.platform.is('mobileweb') ) ) {
          this._currentPlatform = 'mobile';
        } else {
          this._currentPlatform = 'browser';
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-09 08:47

    Ionic-4 Platform specific value

    goto- node_modules@ionic\angular\dist\providers\platform.d.ts

         Platform Name   | Description                        |
     * | android         | on a device running Android.       |
     * | cordova         | on a device running Cordova.       |
     * | ios             | on a device running iOS.           |
     * | ipad            | on an iPad device.                 |
     * | iphone          | on an iPhone device.               |
     * | phablet         | on a phablet device.               |
     * | tablet          | on a tablet device.                |
     * | electron        | in Electron on a desktop device.   |
     * | pwa             | as a PWA app.                      |
     * | mobile          | on a mobile device.                |
     * | desktop         | on a desktop device.               |
     * | hybrid          | is a cordova or capacitor app.     |
    
    0 讨论(0)
  • 2021-02-09 08:57

    The logic used by you is the correct logic.

    The issue is with the ionic 4 and it is returning wrong values.

    Bug has been posted on ionic repo: https://github.com/ionic-team/ionic/issues/15165

    The other issue related to platform coming as ['android'] is also a bug and that has also been reported here: https://github.com/ionic-team/ionic/issues/15051

    0 讨论(0)
  • 2021-02-09 09:00

    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');
                 }
          }}
    
    0 讨论(0)
  • 2021-02-09 09:02

    Ionic 4 / Capacitor

    I have written a service like so:

    detect-platform.service.ts

    import { Injectable } from '@angular/core';
    import { Platform } from '@ionic/angular';
    import { find } from 'lodash';
    
    @Injectable({
        providedIn: 'root'
    })
    export class DetectPlatformService {
    
        isDevice: boolean = false;
    
        constructor(
            private platform: Platform, ) { }
    
        setPlatform(): void {
    
            const platforms: string[] = this.platform.platforms();
            const platform: string = find(platforms, (p: string) => {
                return p === 'capacitor';
            });
    
            this.isDevice = platform ? true : false;
        }   
    
    }
    

    Note: Since I use Ionic 4/Capacitor it gives true if it is on device else false.

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