Skip to content

Latest commit

 

History

History
114 lines (86 loc) · 3.64 KB

File metadata and controls

114 lines (86 loc) · 3.64 KB

npm npm npm Twitter Follow

nativescript-platform

A NativeScript plugin to easily deal with and detect which platform you are on

Developed by

MasterTech

License

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.

Donate Patreon

Requirements

Any version of NativeScript

Installation

tns plugin add nativescript-platform

Usage

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.

You ask, how exactly does this help?

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.

API

Functions

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

Variables

  • .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 */ }

Switch Statement support

.platform

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

.deviceType

Tablet or Phone

.screen

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

.device

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