A NativeScript plugin to easily deal with and detect which platform you are on
This is released under the MIT License, meaning you are free to include this in any type of program -- However for entities that need a support contract, changes, enhancements and/or a commercial license please contact me at http://nativescript.tools.
I also do contract work; so if you have a module you want built for NativeScript (or any other software projects) feel free to contact me nathan@master-technology.com.
Any version of NativeScript
tns plugin add nativescript-platform
To use the module you just require() it:
var myPlatform = require( "nativescript-platform" );
if (myPlatform.android) {
// do android specific stuff
}please note you can also simple do, and then the nsPlatform will be available globally everywhere:
require( "nativescript-platform" );
if (nsPlatform.ios) {
// do ios specific stuff
}nsPlatform will be declared globally. My recommendation is just to include it in the app.js and then use it everywhere.
This wraps up several simple ways to check for the platform you are on. This comes up commonly when you are trying to do stuff on just one platform.
- .isAndroid();
- .isIOS();
- .isWindows(); returns true or false depending on the platform it is on Example:
if (nsPlatform.isAndroid()) { /* do my android specific stuff */ }
- .hasSoftNav(); returns if Android device is using Soft Navigation.
- .ios
- .windows
- .android
- Is set to either true or false for the platform it is on Example:
if (nsPlatform.ios) { /* do my ios specific stuff */ }
- .type.WINDOWS
- .type.IOS
- .type.ANDROID Example:
switch (nsPlatform.platform) {
case nsPlatform.type.WINDOWS: // Do Windows stuff
case nsPlatform.type.IOS: // Do iOS stuff
case nsPlatform.type.ANDROID: // Do Android stuff
}
Tablet or Phone
- .width - Width of Screen (DIP)
- .height - Height of Screen (DIP)
- .scale - Scale of Screen (DIP)
- .widthPixels - Width of Screen (Pixels)
- .heightPixels - Height of Screen (Pixels)
- .emulator - True or False
- .model - Model number of device
- .name - Name of device (might be the same as model)
- .manufacturer - Manufacturer of the device
console.log(nsPlatform.manufacturer, nsPlatform.name);
// Outputs: "Apple iPhoneX"
