Hybrid OR Native App development with JavaScript

When I started planning to write this article the idea was to do some VS comparison of both Apache Cordova and NativeScript. But while I was writing notes for this article I figured that it’s not only the technical aspect for comparing any technology, sometime it’s business requirement which override the technical aspect, also when we are comparing 2 technology stack it’s really helpful if you knows what are the pros and cons of two different tech stack, sometimes it’s not the end result which matters more but the overall process using which you arrive at the end result. So I have changed it from VS to OR.

cordova ORphoto

When we talk about process for creating any kind of projects, it involves both business (all the entities who are involved in human resource process or money aspect of project), there are certain decision which are taken at business level so if they are more clear on problems involved upfront they can take better decision. Technical (developers who will bring the app/project to life), will go through some technical details so that developer can take a better decision on whether to go for Hybrid model using ApacheCordova or NativeScript based upon their strength and technical expectations from the project.

We can broadly define mobile app development in 4 different categories:

  1. Native Apps: using pure languages such as Java, Objective C, Swift or C#, entry level is high, cost of development is high and startup cost can be high based upon availability of good skilled developer in pure languages.
  2. Cross – Compiled: You create your application in language like C# and then using a tool it will compile the same code to native mobile code example Xamarin. (Not purely sure how this works you can visit this website to learn more about it).
  3. Web Based Hybrid model: It’s a kind of hybrid app development technology where app is deployed in container app not on Native Layer tools and languages you need to create web based hybrid apps are PhoneGap/Apache Cordova, Supersonic, Ionic, Bootstrap, AngularJS, jQuery Mobile. Basic requirement to create app is you should know HTML5, JavaScript and CSS (all the skills which a web site developer has to know). So this hybrid model of development brings down the entry level barrier for mobile app developers. Will discuss the pros and cons in detail later.
  4. Native App development using JavaScript runtime: Mobile app is created in JavaScript and runs in JavaScript Virtual Machine. Some of the examples are Appcelerator Titanium, Facebook react Native, Telerik NativeScript. Both Appcelerator Titanium and Facebook React Native use API wrapper to do the communication between App Code and Native Layer. NativeScript doesn’t use any wrapper and its open source as well. Will focus on only NativeScript in this model.

Focus of this article is last 2 ways of creating mobile apps. Both model of app creation requires a developer with good JavaScript knowledge i.e. the better you know how JavaScript works/behave the better app you can create on performance level).

Let’s first understand the technical aspect of both Hybrid and Native App development using JavaScript:

Hybrid App development or Web Based app development using JavaScript:

  • User interface is created and styled through HTML5 and CSS3 and JavaScript
  • An instance of platform-specific browser (Web View) is embedded within the application and is used to visualize the HTML and CSS
  • Cordova (Phone gap) provides a “native bridge” to access the native resources on the device.
  • Cordova plugins are used to provide access to some of the native APIs in the underlying platform, most of the dynamic functionality or plugin code is handled using JavaScript.
  • Resulting applications are hybrid meaning that they are neither truly native mobile application (as all layout rendering is done via Web views instead of platform’s native UI framework) nor purely Web-based (as they are not just web-apps, but packaged as apps for distribution and have access to native device APIs).
  • User Input is handled by the embedded browser instance and delegated further as JavaScript events to the currently focused DOM element.
    • WebView :-
    • WebView is a chromeless browser which is configured to run in full screen mode.
    • All hybrid mobile app frameworks rely on standard implementation of WebViews for the app presentation tier.
    • There are different types of WebViews available depending on the framework we choose.
    • The System WebView is a native component provided by the operating system to be able to load web content

NativeScript App development using JavaScript:  NativeScript is unique because it allows you to access the native elements of the host platform via JavaScript. It allows access to full operating system libraries and third-party libraries from JavaScript code.

  • User interface is created using XML based layout language and styled through stripped down version of CSS3.
  • All business logic such as data models, view models/controllers etc. can be written in JavaScript.
  • NativeScript has set of libraries that enables calling APIs in the Android and iOS framework using JavaScript code.
  • It’s built upon several major parts including a JavaScript virtual machine to interpret and execute the JavaScript code and a “bridge” module is included to translate the calls to underlying platform specific APIS or marshaling service that handles data transfer to/from JavaScript to/from native layer.
  • Virtual machine used on Android is Google’s V8 and on iOS 7.0+ is WebKit’s JavaScriptCore.

NativeScript Apps vs Hybrid Apps:

  • Major difference between the two types of app creation is UI Stack and User Input.
  • UI are instances of the corresponding native widgets/visual – e.g. android.widget.Button on android or UIKit.UIButton on iOS whereas in Hybrid apps UI instances are HTML5 instances which are rendered based upon HTML5 rendering in the WebView supported on particular platform.
  • NativeScript Runtime provides full access to the underlying native APIs such as Camera, Location, File System etc. In Hybrid app development access to native APIs is through Cordova Plugins and HTML5 feature set’s available in the particular WebView in the platform.
  • User Input is handled by native handlers (delegates) declared in JavaScript – e.g. View.OnClickListener or UIControl.addTarget implementation whereas in Hybrid mode it’s all being handled by the WebView.
  • Ease of development if you are more at ease with development for websites using desktop browser with your editor where you make a change in editor and you can see the changes using some livesync or real time update of view in browser, or you can use desktop browser to tweak/make UI changes with dev. tools in your Chrome/Firefox browser. You can do almost all those things if you are creating your app using phonegap.
  • With Nativescript you can debug your app and push the changes using a tns livesync command with –watch mode but the only problem I found was I was not able to make real time UI updates or I was not able to debug the app in browser view using chrome:\\inspect so you have to be bit extra careful with your UI design.
  • If you are familiar with MXML (UI schema used by adobe to create Flex) or XAML a schema used by Microsoft to for creating UI interface for Silverlight apps. You will be at ease with the XML schema Nativescript is using for its UI layer. Or if you have done any development for Android using Java then you are good. In Phonegap you can create your UI layer same way as you would do for your desktop website. The only catch from my existing experience with doing that is the markup schema for desktop web apps is bit complex and you should avoid mapping exact same schema for mobile app it drag down the speed.

Performance:

  • NativeScript implementation: NativeScript is adding additional layer of abstraction via JavaScript code interpretation and data marshaling. So it will definitely not going to be as fast as apps created in pure Native code directly. But it’s definitely faster than hybrid apps.
  • Hybrid implementation:
    • Use of web-based technologies leads some Apache Cordova applications to run slower than native applications with similar functionality.

Adobe Systems warns that application maybe rejected by Apple for being too slow or not feeling ‘native’ enough. This can be issue for some Apache Cordova Applications.

Business aspect of creating mobile apps using either of model: For business (especially small organizations) the call is always if more developers are available to work in particular technology or not. In this case both models (NativeScript or Hybrid model) requires good knowledge of JavaScript.

If you have some good UI developers who can create nice optimized mobile UIs in HTML/CSS3 for you then you have good combination of team which can create good looking apps for you using Apache Cordova/Hybrid model of mobile app development as the learning curve in moving from desktop website development to mobile app development is not that steep. Same team can create mobile apps using Apache Cordova framework but focus has to be on optimized UIs as my experience with Cordova apps suggest that most of the time it’s the bloated UIs which drags the performance of the app and make it unusable in most of the cases.

In case of NativeScript as the UI is not HTML5 based and you can’t use all the CSS3 functionalities your JavaScript developers has to be little more experienced with UI stuff as well so that they can use XML based UI and manage it through JavaScript. Learning curve here is bit high and your normal web development team can’t be put straight for mobile app development in NativeScript. Even if you are good with JavaScript as a Freelance consultant you have to learn a bit of XML based UI designing with all the stripped down CSS3 styling you can use, here if you are OLD Flex/Sliverlight developer or you have done some of mobile app development using XML UI creation for Android/iOS with native app development you are good to go for NativeScript mobile app development as well.

In this article I tried to cover the two mobile app development models and I tried to share my experience on how we should approach different model, it can vary from team to team or organization to organization. The idea is to know it better before you pick any tech stack  for development. All the Best with your mobile app development.

Nativescript app with Angular2: Listing phone contacts

This is a phone contacts listing app created using Nativescript and Angular. The concept for the app is almost similar to the other app I have created using PhoneGap where I have used Phonegap/Cordova plugins to fetch phone contacts and list them, you can get that project from github. In this app I am using Nativescript plugin to fetch phone contacts and list them. (currently it doesn’t have search functionality as the one I have in PhoneGap app).

If you are new to Nativescript I would suggest you first go through a nicely written tutorial post by TJ VanToll and linked repo on github. This tutorial has covered lot’s of basics for both Anuglar2 and Nativescript developers.

I will be sharing my learning during creation of this app, you can find the repository on github

I will not walk through the project step by step but I will list some of the lessons I have learned in this project and how I was able to resolve the issues I came across.

  • Naming convention is important: When you are creating your templates in xml/html (you can name the template file as html as most of the editors will give you better help) make sure you either use CamleCase or kebab-case styling to name the tags for e.g. either <StackLayout> or <stack-layout> not like <stacklayout> or <textfield> I was using all small case with any hyphen and for some reason my ngModel was not working. I tried to raise this issue at multiple places but couldn’t find any answer to it. But I see in one of the tutorial its updated now that you should be using either of the 2 naming conventions to name your tags in xml/html template markup nothing else or you might be wasting lot of time like I did.
  • Permission model for Android SDK 23: if you are developing app using android SDK 23 and you are using device features for which you need user permission. You have to initiate permission dialog from within the app when you will be initiating the call for that functionality and only on it’s successful acceptance you can do that activity on phone. To achieve this functionality in your nativescript app you have to take help of nativescript-permission plugin created by Nathan a very senior developer and very approachable. He helped me on Nativescript slack channel to quickly set this plugin up for Typescript as it’s originally created and setup for JavaScript only, and I was having problem in using it my Angular/Typescript Nativescript app.
    other than what’s listed in plugin usage on npmjs page you have to declare android variable in your typescript project to use it. Something like

    declare var android:any;
  • Use fat arrow within your promises: when you will use .then() in your promise you can’t use “this” access variables declared at class level or functional level because of scoping issue. Your best bet is to use fat arrow “=>” and then use this to access local variable declared at class or outer functional level. It will save lot of headache on figuring out how to return value to variable outside or how to access variable inside .then().
  • Debugging Nativescript app: you can debug your nativescript app using visual studio code integration with debug option, you have to install a Nativescript extension for visual studio code and go through the article on how to debug Nativescript app. But I found it hard to make design tweaks in debug mode as I can do in phonegap/cordova apps in Google chrome using chrome://inspect/#devices but I think that’s the trade of you have to do when you want to go to native level.
  • Useful Nativescript commands: The one I liked most is “tns livesync –watch” it just don’t compile whole code again on every save, it just push the differential on every save. Only issue is sometimes you will suddenly see whole set of error on your screen not because of error in code, but for reasons other then change in code, like the view on the particular page is getting derived from some value from previous screen and when it refresh the view it doesn’t have the value.
  • Nativescript Contacts plugin: For fetching contacts from my android device in this nativescript app I am using nativescript – contacts.
  • As is the case with Permission plugin this plugin also returns promise. all the possible usage of the plugin are explained in detail with proper example. I am just using getAllContacts API.
  • Other good thing I liked in this plugin detail is that author has given a complete data structure  returned by the API. So you can pick what you need.
  • If you are new/beginner to Nativescript and Angular2 development I will suggest you should use use the Nativescript tutorial chatper 1. This chapter covers all the aspect of installing Nativescript, and you can use Nativescript CLI to create project from command line and use –ng to set it up with angular and typescript e.g.
    tns create my-app-name –ng 
  • I have setup contact listing app using default project created with Nativescript CLI as a base. It saves you from lot’s of other details of setting up and preparing your project to be used with npm modules and compilation of native apk file.
  • The idea of this project was that someone who wants to list all contacts or do some manipulation with contacts on phone where the app is install can be done.

I hope you find this post and project useful. Have fun coding apps in Nativescript to get maximum performance without coding a single piece of line in Java or Objective C.