Search code examples
cordovaionic-frameworkionic3cordova-pluginsionic-native

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':

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? For Android and iOS is the platform cordova? Should I also be writing if check for this.platform.is('Android') and this.platform.is('iOS') also?


Solution

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

    Now to answer your questions

    when this runs on a real device, what exactly is the output of the 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.

    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.

    For Android and iOS is the platform cordova?

    Yes, only if you have created the native application( .apk , .ipa ) from cordova framework.

    If you are accessing the web application in the browser from your mobile device, then the platform is not cordova.

    Should I also be writing if check for this.platform.is('Android') and this.platform.is('iOS') also?

    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.

    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/