This article explores NativeScript, an open source framework for building truly native mobile apps.
Installation and a demo
NativeScript is built on Node.js; so if you don’t have the latter, you need to install it. Installation is very simple. Go to the official website of Node.js, and get the installation file. Double click on it and install it. The link for the official website is https://nodejs.org/.
To check whether Node.js is already installed or not, you can use the following command:
If you get an error message like ‘No command found’ or ‘Command is not recognised’, it means you have not installed Node.js. After installing it, the next step is to install NativeScript, for which you can use the following command:
npm install -g nativescript
To verify whether NativeScript has been successfully installed, we can run the following command:
Here, ‘tns’ stands for Telerik NativeScript since Telerik has developed this framework. If you see a lot of options as an output, it means you have successfully installed NativeScript.
After installing NativeScript, the next step is to install the requirements for iOS and Android. To build native iOS and Android apps, set up the development environment. Here, tns provides a quick start script which will help us to install everything.
Setting up the development environment in Windows: Press the Start button, type cmd, and then right-click and run as an administrator, going on to click the Enter button. Next, run the following script at the CLI prompt. Make sure that you run it as an administrator; else, it will fail or give some error.
@powershell -NoProfile -ExecutionPolicy Bypass -Command “iex ((new-object net.webclient).DownloadString(‘https://www.nativescript.org/setup/win’))”
The script will take some time to install everything. Once it is done, you can close the CLI and open it again to refresh it.
After installation, we will have the following:
Node.js (LTS) 8.x or later Google Chrome JDK 8 Android SDK Google Repository Android SDK build-tools Android Studio
The script will work for the Android environment in Windows, and if you want to develop iOS apps in Windows, then NativeScript Sidekick has to be installed. The link for this is https://www.nativescript.org/nativescript-sidekick.
Due to time and space constraints, we are skipping this stage.
Setting up the development environment in Mac systems: If you are using the Mac OS, then you can run the following quick start script:
ruby -e “$(curl -fsSL https://www.nativescript.org/setup/mac)”
The script needs administrative access to run many commands, so while executing it, you will often be asked for the password. Once done, all iOS app development prerequisites will be installed.
The next step is to verify the setup. We can use the following command to do so:
If no issue is detected, it means everything is perfect and we can start our first ‘Hello world’ kind of application.
To create an app, run the following command:
tns create HelloOpenSource4u --template nativescript-template-tutorial
Here, the tns create command will build an app with the name ‘HelloOpenSource4u’ and it will use a default template provided by NativeScript named ‘nativescript-template-tutorial’. There are other templates also available and you can create your own too. The link for that is https://github.com/NativeScript/NativeScript/wiki/Using-the-%60tns-create%60-command.
The Create command will take some time, as it fetches all the dependencies and creates a folder with a given app name. Once it is done, go to the folder. In our case, it’s ‘HelloOpenSource4u’.
Here is the command to run it:
cd HelloOpenSource4uFigure 2: App demo
Running the app
To start the application, we need to use the tns run command inside the folder, as follows:
tns run android
It will take a few seconds, as NativeScript CLI will build and deploy a native Android application. When the command finishes, the native emulator will open and the app will be seen.
To run the application for iOS, run the command given below:
tns run ios
Figure 2 shows what happens after the app is run.
The advantages of using NativeScript are:
- It’s a hybrid mobile app development platform in which Android and native iOS apps without Web views can be created. It supports cross-platform development.
So give it a try when you develop your next mobile app.
source : http://opensourceforu.com