简体   繁体   English

离子本机插件-平台检查-防止通过“离子服务”运行时浏览器中的代码中断

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

I am new to ionic development and am using the below code (on button click event) to ensure that the native plugin call code doesn't break/error out when executing on web browser using 'ionic serve': 我是ionic开发的新手,正在使用以下代码(单击按钮时发生事件)来确保在使用“ ionic serve”的Web浏览器上执行时,本机插件调用代码不会中断/出错:

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..

My question is - when this runs on a real device, what exactly is the output of the if check? 我的问题是-当它在真实设备上运行时,if检查的输出到底是什么? For Android and iOS is the platform cordova? 适用于Android和iOS的平台是cordova吗? Should I also be writing if check for this.platform.is('Android') and this.platform.is('iOS') also? 是否还要检查this.platform.is('Android')和this.platform.is('iOS')吗?

Depending on the platform the user is on, is(platformName) will return true or false . 根据用户所处的平台, is(platformName)将返回truefalse Note that the same app can return true for more than one platform name. 请注意,同一应用可以为多个平台名称返回true For example, an app running from an iPad would return true for the platform names: mobile , ios , ipad , and tablet . 例如,从iPad运行的应用将针对以下平台名称返回truemobileiosipadtablet 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 . 此外,如果该应用程序是从Cordova运行的,那么cordova是正确的;如果它是从iPad上的Web浏览器运行的,那么mobileweb将是true

Now to answer your questions 现在回答您的问题

when this runs on a real device, what exactly is the output of the if check? 当它在真实设备上运行时,if check的输出到底是什么?

If you are application(android,ios,windows) is built using the cordova framework and installed into the respective devices, then this.platform.is('cordova') would return true. 如果您是使用cordova框架构建应用程序(android,ios,windows)并安装到相应设备中的,则this.platform.is('cordova')将返回true。

On the other hand, If you host the application on any web server and try accessing it through your mobile browser, then this.platform.is('cordova') would return false. 另一方面,如果将应用程序托管在任何Web服务器上并尝试通过移动浏览器访问它,则this.platform.is('cordova')将返回false。

For Android and iOS is the platform cordova ? 适用于Android和iOS的平台是cordova 吗?

Yes, only if you have created the native application( .apk , .ipa ) from cordova framework. 是的,仅当您从cordova框架创建了本机应用程序(.apk,.ipa)时。

If you are accessing the web application in the browser from your mobile device, then the platform is not cordova. 如果要从移动设备访问浏览器中的Web应用程序,则说明该平台不是cordova。

Should I also be writing if check for this.platform.is('Android') and this.platform.is('iOS') also? 是否还要检查this.platform.is('Android')和this.platform.is('iOS')吗?

Depends on the situation, In most of the cases if you have generated the native application( .apk, .ipa ) from cordova framework and you have some generic code for both the platforms then this.platform.is('cordova') is enough. 根据具体情况,在大多数情况下,如果您是从cordova框架生成本机应用程序(.apk,.ipa)的,并且对于这两个平台都有一些通用代码,那么this.platform.is('cordova')就足够了。

You can check the following table for reference 您可以查看下表以供参考

 | 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.       |

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM