如何使用 Ionic 4 检测平台

Dar*_*ana 8 platform typescript ionic-framework angular ionic4

如何使用 Ionic 4 检测浏览器和移动网络平台,因为当我在桌面浏览器中尝试使用以下代码时,它并未处于“核心”状态。

if (this.platform.is('core')) {
    alert('core platform');
  } else {
    alert('something else');
  }
Run Code Online (Sandbox Code Playgroud)

当我在 chrome 开发人员工具中进行调试时,它按照下面的快照显示“android”平台。

在此处输入图片说明

任何人都可以帮助我如何检测 Ionic 4 中的平台,或者有什么替代方法?

小智 7

对于我的用例,我想要一些东西来区分nativebrowser平台。也就是说,我的应用程序是在浏览器中运行还是在本机移动设备中运行。这是我想出的服务:

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';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*kar 5

以下链接可能对您有所帮助:https :
//forum.ionicframework.com/t/how-to-determine-if-browser-or-app/89149/16

或者您可以使用以下方法:

public isDesktop() {
    let platforms = this.plt.platforms();
    if(platforms[0] == "core" || platforms[0] == "mobileweb") {
        return true;
    } else {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)


Duc*_*ang 5

目前 Ionic 4 支持平台检测。以下代码对我有用。

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');
             }
      }}
Run Code Online (Sandbox Code Playgroud)


小智 5

Ionic-4 平台特定值

转到-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.     |
Run Code Online (Sandbox Code Playgroud)