如何使用 Ionic 4 检测浏览器和移动网络平台,因为当我在桌面浏览器中尝试使用以下代码时,它没有落入‘core’健康)状况。
if (this.platform.is('core')) {
alert('core platform');
} else {
alert('something else');
}
当我在 chrome 开发者工具中进行调试时,它显示'安卓'平台如下快照所示。
谁能帮助我如何检测 Ionic 4 中的平台或者有什么替代方案?
对于我的用例,我想要一些东西来区分native
and browser
平台。也就是说,我的应用程序是在浏览器还是本机移动设备中运行。这是我想出的服务:
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';
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)