Ionic 本机插件 - 平台检查 - 防止 运行 通过 'ionic serve' 时浏览器中的代码中断

Ionic native plugin - platform check - to prevent code break in browser when run via 'ionic serve'

我是 ionic 开发的新手,我正在使用下面的代码(在按钮单击事件上)来确保使用 [=17= 在 Web 浏览器上执行时本机插件调用代码不会 break/error ]:

if (!this.platform.is('cordova')) {
    console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
    return;
  }
--other wise run the native code..

我的问题是 - 当它在真实设备上运行时,if 检查的输出到底是什么?对于 Android 和 iOS 是平台 cordova 吗?如果还检查 this.platform.is('Android') 和 this.platform.is('iOS') 我是否也应该写信?

Depending on the platform the user is on, is(platformName) will return true or false. Note that the same app can return true for more than one platform name. For example, an app running from an iPad would return true for the platform names: mobile, ios, ipad, and tablet. Additionally, if the app was running from Cordova then cordova would be true, and if it was running from a web browser on the iPad then mobileweb would be true.

现在回答你的问题

当它在真实设备上运行时,if 检查的输出到底是什么?

如果您的应用程序(android,ios,windows)是使用cordova框架构建并安装到相应的设备中,那么this.platform.is('cordova') 会 return 为真。

另一方面,如果您在任何网络服务器上托管应用程序并尝试通过移动浏览器访问它,那么 this.platform.is('cordova') 将 return 错误。

对于Android和iOS是平台cordova?

是的,前提是您已经从 cordova 框架创建了本机应用程序(.apk、.ipa)。

如果您是通过移动设备在浏览器中访问 Web 应用程序,则该平台不是 cordova。

如果检查 this.platform.is('Android') 和 this.platform.is('iOS') 我也应该写吗?

视情况而定,在大多数情况下,如果您从 cordova 框架生成了本机应用程序(.apk、.ipa)并且您有一些适用于这两个平台的通用代码,那么 this.platform.is('cordova') 够了。

您可以查看以下table以供参考

 | Platform Name   | Description                        |
 * |-----------------|------------------------------------|
 * | android         | on a device running Android.       |
 * | cordova         | on a device running Cordova.       |
 * | core            | on a desktop device.               |
 * | ios             | on a device running iOS.           |
 * | ipad            | on an iPad device.                 |
 * | iphone          | on an iPhone device.               |
 * | mobile          | on a mobile device.                |
 * | mobileweb       | in a browser on a mobile device.   |
 * | phablet         | on a phablet device.               |
 * | tablet          | on a tablet device.                |
 * | windows         | on a device running Windows.       |

参考:https://ionicframework.com/docs/v3/api/platform/Platform/