How to detect platform using Ionic 4

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

提交回复
热议问题