如何使用Ionic 4检测浏览器和mobileweb平台,因为当我在桌面浏览器中尝试使用以下代码时,它不会处于“核心”状态。
if (this.platform.is('core')) {
alert('core platform');
} else {
alert('something else');
}
当我在chrome开发人员工具中进行调试时,它会根据下面的快照显示“android”平台。
任何人都可以帮助我如何检测Ionic 4中的平台或者可以替代这个平台?
以下链接可以帮助您: 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;
}
}
目前,Ionic 4支持平台检测。以下代码适用于我。
this.platform.ready().then(() => {
if (this.platform.is('android')) {
alert('android');
} else if (this.platform.is('ios')) {
alert('ios');
} else {
// fallback to browser APIs
}
});
您使用的逻辑是正确的逻辑。
问题在于离子4,它返回错误的值。
Bug已发布在离子回购:https://github.com/ionic-team/ionic/issues/15165上
与['android']平台相关的另一个问题也是一个bug,这里也有报道:https://github.com/ionic-team/ionic/issues/15051
Ionic-4平台特定值
goto- node_modules @ ionic \ angular \ dist \ providers \ platform.d.ts
平台名称|说明|
* | 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. |