Native Script
Native Script
Introduction To NativeScript
User Interface
Course Summary
What is NativeScript?
Open Source Framework backed by the company Telerik.
o No DOM.
Use Angular, TypeScript or JavaScript.
o No Objective C, Swift and Java.
o No cross compiler like Xamarin
If you want to use Angular, great! If you want to use just JavaScript, no problem! You can
even build your app in TypeScript or Babel if you want.
NativeScript Vs Hybrid
Both build apps for multiple mobile platforms from a single source code.
Xamarin is a better suit for engineers who prefer to write code in C#. For developers who
prefer JavaScript, TypeScript or Angular2, NativeScript is the proper choice.
If the app contains complex UI elements, React Native will be the right option. However, if the
developer needs a single code base for cross-platform app, NativeScript can satisfy their
demands.
Quick to learn and Skills reuse: Accelerate the learning curve by using existing web
development skills.
Strudel
MeWatt
Herd of Sheffields
tripist
LiveShopper etc.
Limitations
It is a rapidly maturing product that is evolving in realtime. So you need to keep your
codebase up to date to ensure that you are leveraging all the new features that come
with each release.
Since it needs more time to grow a decent 3rd party community, the number of
components available are limited.
Still lacking some UI components and options.
```sh
```
```
tns doctor
```
If you see `No issues were detected`, you are good to go!
```sh
cd HelloWorld
```
```sh
```
```sh
```
```sh
tns run android --emulator
```
What is 'tns'?
In NativeScript you develop and build apps using the NativeScript CLI. After installation, the
NativeScript CLI is available on your terminal or command prompt
as tns (Telerik NativeScript ) or nativescript command.
Choosing an editor
You can develop NativeScript applications in any text editor or IDE you wish. Therefore, if
you’re deeply committed to an editor, by all means continue to use your editor of choice as
you build applications with NativeScript.
Directory Structure
The project folder contains 3 main sub-folders: app, lib and platforms.
platforms folder contains all the specific platform code and libraries for each target
platform that are needed for compiling your app for that specific platform.
We highly recommend you to practice as you learn, for a better understanding of the
concepts.
Layouts
It is a special UI element that instructs and informs your app how to organize the UI
elements on a mobile device’s screen. It affect the location and arrangement of UI elements
placed within the layout.
Different types of Layouts
are AbsoluteLayout, GridLayout, FlexboxLayout, DockLayout and WrapLayout.
Types of Layouts
FlexboxLayout is an implementation of the CSS Flexible Box Layout.
DockLayout arranges its children at its outer edges and allows its last child to take up the
remaining space.
Styling the UI
The NativeScript CSS has some differences from the normal CSS, but overall, it works pretty
similar to how a browser does.
Typically, each app has one frame at the root level - the topmost frame. To navigate between
pages, you can use the navigate method of the topmost frame instance.
There are several ways to perform navigation; which one you use depends on the needs of
your app.
topmost.navigate("details-page");
page.content = label;
return page;
};
topmost.navigate(factoryFunc);
How to pass content between different pages
During Navigation, you can pass context to the page with a NavigationEntry object and it
provides a finer control over navigation. For example, with NavigationEntry you can
also animate the navigation.
var navigationEntry = {
moduleName: "details-page",
animated: false
};
topmost.navigate(navigationEntry);
Clear History: You can navigate to a page by clearing the all navigation history by
setting clearHistory to true. This is very useful for multiple-page authentication process.
var navigationEntry = {
moduleName: "main-page",
backstackVisible: false,
clearHistory: true
};
topmost.navigate(navigationEntry);
Clear History: You can navigate to a page by clearing the all navigation history by
setting clearHistory to true. This is very useful for multiple-page authentication process.
var navigationEntry = {
moduleName: "main-page",
backstackVisible: false,
clearHistory: true
};
topmost.navigate(navigationEntry);
Here is the UI
```sh
```
path: "nested-routers",
component: NestedRoutersComponent,
children: [
];
```
Example : First Screen with a list of Books and on clicking of each book directs to detailed
description ( author , language, price, publisher etc. ) of the book.
Available Plugins
There are many NativeScript plugins available and you can find the list of those plugins on
the Telerik Verified Plugins.
Camera
Location
SQLite
Tooltip
Accordion etc
To use a plugin inside your project, you need to add a require in your app.
Location
To include geolocation in your app, use the nativescript-geolocation plugin, accessible
via npm. Here is an example, which shows how to use the specific plugin.
To make the plugin available in your app, run the following command:
<Page>
<StackLayout>
</StackLayout>
</Page>
function GetLocationTap(args) {
then(function(loc) {
if (loc) {
}, function(e){
console.log("Error:" + e.message);
});
exports.GetLocationTap = GetLocationTap;
Camera
The NativeScript camera plugin was intended for the initial two sections of the job( taking a
photo and optionally saving to device storage).
Requesting permissions
Newer API levels of Android and iOS versions are requiring explicit permissions in order the
application to have access to the camera and to be able to save photos to the device. Once
the user has granted permissions the camera module can be used.
camera.requestPermissions();
The technique isAvaiable will return truer if the camera hardware is ready to use or false if
otherwise.
var isAvailable = camera.isAvailable();
camera.takePicture()
.then(function (imageAsset) {
image.src = imageAsset;
}).catch(function(err) {
console.log("Error:" + err.message);
});
What are some of the best ways to store data locally with a NativeScript app?
SQLITE
Google Firebase
Couchbase
Local Storage
File System etc.
SQLITE
NativeScript module providing sqlite actions for Android and iOS.
Application Lifecycle
NativeScript apps have following life cycle events.
Event
It is a message sent from an event emitter to signify the occurrence of a specific action.
This event can be generated by a user action or by program logic.
The object that raises the event is called an event sender or event raiser. The object that
consumes the event is called an event listener or event handler.
Below example sets a function that prints a "Tapped!" message in the console, when a button
is tapped.
var testButton = new buttonModule.Button();
testButton.text = "Test";
console.log("Tapped!");
};
testButton2.removeEventListener(buttonModule.Button.tapEvent);
Data Binding
It is the process of connecting application UI to the code. Data flow direction is the way
data flows.
One-Wayis the default setting that ensures the target property updates when a change
in the source property occurs.
Two-Way setting ensures the reflection of changes in both directions —
from targetto source and vice versa.
Binding to an event in XML
There is an option to bind a function to execute on a specific event. This option is available
only through an XML declaration. To implement such a functionality, the source object should
have an event handler function.
You can start the debugging, when you launch your application using the below command.
The second way is if your application is currently running, you can use the below
command, to connect to an already running application.
This will build you a release version of your APK. The APK's location is going to be
in /platforms/Android/build/outputs/APKs.
You need a key to sign your application so that Google (and others) and you can follow the
instructions at Google Play Store.
Publishing to iOS
On iOS, it is a slightly different process. The first step is to run below command from the
terminal.
tns build ios --release
The main XCode project, the application file is located in the /platforms/ios/ folder that you
will need to open via XCode to submit to the Apple iOS Store.
Then, you can follow the instructions that Apple detail at developer.apple.com.
Introduction
Getting started with Nativescript
UI and Navigation
Extending With Plugins
Events with Data Binding
Debugging and Publishing
However, NativeScript has a bunch of other components that is not covered in this course. If
you want to get in deep with NativeScript, the better to place to start is Telerik NativeScript
Docs.
Happy NativeScript-ing!
Quiz
1. How to install NativeScript CLI?+A1:B28 NPM INSTALL -G NATIVESCRIPT
2. Can a developer build an app using NativeScript, if he/she doesn't know Java or Objective C/Swift?
YES U CAN DEVLOP a native script
3. tns doctor' command is used to __________. VERIFY THE SET UP AND INSTALLATION
4. NativeScript is backed by __________. TELERIK
5. The source code resides in the ________ folder. APP
6. __________ positions its children in rows or columns, based on the orientation property, until the
space is filled and then wraps them on a new row or column. WRAPLAYOUT
7. In NativeScript, the UI is written in _________ and coding is done with __________. XML,
JAVASCRIPT/
8. How many types of selectors are supported in NativeScript? 3
9. __________ is the root-level container for your app's UI and you can use it for navigation inside your
app. TOPMOST
10. Select one which is not a layout type of NativeScript. WRAPPERLAYOUT
11. To include geolocation in your app, which command has to be executed? TNS PLUGIN ADD
NATIVESCRIPT-GEOLOCATION
12. An event can be generated by __________. BOTH
13. What command has to be executed for adding a standard npm dependency for camera plugin? NPM
INSTALL NATIVESCRIPT-CAM--SAVE
14. camera.isAvailable() method will return false when used in iOS simulator/Android Emulator. T
15. In 'npm install nativescript-camera –save' command, the --save flag will add the plugin as dependency
in your package.json file. T
16. Select the app that developed in NativeScript? ALL
17. Cross compiler is present in NativeScript like Xamarin. F
18. Can developer use platform-specific UI controls with NativeScript approach? YES
19. What is tns? TELERIK NATIVESCRIPT
20. You can navigate to a page without adding the navigation to the history by setting backstackVisible to
true. F(0)
21. NativeScript has cross compiler like Xamarin. F
22. camera.isAvailable() method will return false when used in iOS simulator/Android Emulator. T
(#Spreadsheet::Formula:0x00559ac37052c0)
23. tns debug android --debug-brk [--device / --emulator / --geny]. This command is used for __________.
debugging, when app launch happening
24. main-page.js defines the __________. APPLICATIONLOGIC
25. __________ has the default UI code. MAIN-VIEW.MODEL
26. Lib folder contains the __________ and dependencies to other platform specific libraries.
lib folder contains the Runtime's native C++ implementation
27. How to create your an project in NativeScript? CREATE
28. __________ arranges its children horizontally or vertically with the orientation property. STACK
LAYOUT
29. How to add platforms (android/ios) to your project in NativeScript? tns platform add android, tns
platform add ios
30. __________ file is the entry point to the application. main-page.js
31. JavaScript arrays map to specialized __________ on Android and __________ on iOS. JAVA ARRAY,
NS ARRAY
32. To write business/logic applications, developer wants a local database. Is there any option to have the
same database in all platforms? YES
33. NativeScript can be described as write once, run anywhere. T
34. NativeScript provides single codebase for both android and iOS. T
35. Which of the selector type has highest priority in NativeScript CSS? ID
36. Native Components of TextField are ________ for Android and _________ for iOS.
anroid.weidg.edittext
37. What are all the three selector types supported by NativeScript All