JavaScript For Native Apps
Written by Ian Elliot   
Tuesday, 10 March 2015

NativeScript is a system that allows you to write native apps for Android, iOS and Windows Phone using JavaScript - and no this isn't a wrapper for an HTML app.

You may have encountered frameworks like PhoneGap/Cordova that enable you to run Web apps as if they were native apps, but NativeScript follows Microsoft's approach - you can use different languages and even markup to create true native apps. At the moment Android is mainly Java and iOS is mainly Objective C/Swift. 

 

nativescriptpanel

 

NativeScript is standard JavaScript that has been extended with some clever middleware that allows you to call functions in the native API.

This sounds complicated, but it actually fairly straightforward. You make a full reference to the native API function, for example 

var file=new java.io.File(path);

and this sets up a call to the Java API function File.

The clever part is that the JavaScript string that specifies the path is automatically converted to a java.lang.String object for the call. The Java File object as attached to the JavaScript variable file in the sense that any attempt to access properties on file are passed on to the Java File object. That is, file acts as a proxy for File. 

The JavaScript is interpreted using the standard JavaScript engine V8 for Android and webkit for iOS and Windows Phone.  Notice, however, that no wrapper is used - there is no browser component building the UI for you from HTML. If you want a button then you have to write JavaScript that calls the native class constructor.

At this point you may start to think that this is great but wait - I still have to write three versions of my app. One using Android native calls, one using iOS native calls and one using Windows Phone native calls. This means your only advantage is that you can make use of JavaScript for all three and this is possibly a language you know. The advantage of using a wrapper is that you are essentially writing a web app, i.e something that could run in a browser, and then using a wrapper that is essentially a modified browser to make it look a bit like a native app. 

 

natviescript

 

NativeScript can help you write just one app for all three platforms using a set of abstractions by way of the NativeScript Modules layer that map onto the native APIs. So instead of specifically calling the API function for File you can simply write:

var file=File(path);

and the abstraction layer will map the JavaScript to the correct native call.

The danger with adopting any scheme like the NativeScript Modules is that making any such abstractions complete is difficult and this means that there are always things you cannot do. In the case of NativeScript this isn't such a worry because you can always fall back on direct API calls.

 

 

The NativeScript system is built on Node.js and to use it you still need to install parts of the native system you are working with - XCode, for example. So you still need some skill in using the native SDKs.

You might also be worried about the overheads in using all of these translations between systems, but it is clamed that it only slows things down by about 10%. 

NativeScript has been developed by Telerik, known for its wrapper approach to developing "native" apps with JavaScript. The surprise is that NativeScript is open source under the Apache 2.0 licence. Presumably Telerik hopes to make a profit on supplying extras such as Telerik Platform Services

If this can be made to work, and if the performance loss is only about 10% as claimed, then this is a useful alternative way of creating native apps. It is hoped that a Version 1 will ship in April. 

 

nativescripticon

Banner


Google Updates Responsible AI Toolkit
01/11/2024

Google has announced updates to the Responsible Generative AI Toolkit to enable it to be used with any LLM model. The Responsible GenAI Toolkit provides resources to design, build, and evaluate open A [ ... ]



Apache Releases Tomcat 11
07/11/2024

Apache has announced the release of Tomcat 11, as well as marking the 25th anniversary of the first commit to the Apache Tomcat source code repository since becoming an ASF project.


More News

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Wednesday, 06 May 2015 )