Ionic
Ionic
Cristian Olaru
This book is for sale at http://leanpub.com/ionic4
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Introduction to Ionic 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii
Introduction to the book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii
Web will be the new native . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
What is Ionic 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
JavaScript is evolving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi
Mobile development with Ionic 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Mobile devices and mobile operating systems . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
Mobile App stores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
How this book is organized
We have split this book in two parts.
Ionic 4 Essentials
Ionic 4 Advanced
This book is a work in progress created using the Lean Pub² self-publishing system. You
can buy a copy of it at a small price when it is still not finished, and you will get the
updated chapters after at the initial price. As the time is passing and new chapters are
added, the price can increase for the new buyers.
Ionic 4 Vision
We try in this book to implement an application according to the new Ionic 4 vision to write once
run everywhere. So we have to write one Web application and we can host it as a static Web app
¹https://ionicframework.com
²https://leanpub.com/ionic4
Introduction to Ionic 4 iii
(PWA), package it and publish it to the mobile app stores, or package it as a desktop application and
let the users download and install it.
This book isn’t a tutorial of Ionic 4 - you have the Ionic 4 documentation site³ for this.
What is Ionic 4
Ionic 4 in its essence is a set of standard Web Components that are rendered according to the design
guidelines Materila Design⁴ for Web and Android devices and Human Interface⁵ for iOs devices.
These web components are created by Ionic team using Stencil JS.
JavaScript is evolving
JavaScript is the language used by Ionic.
¹⁰https://www.polymer-project.org/3.0/docs/about_30
¹¹https://stenciljs.com/
Introduction to Ionic 4 vii
JavaScript
JavaScript is the language used by de developers for giving dynamism to the Web applications that
are running in the browser (CSS is used for styling them). In general, there is a compatibility between
the JavaScript implementations in the modern browsers (Google Chrome, Firefox, Apple Safari,
Microsoft Edge), mainly because all are implementing a specification named ECMAScript which
is responsible for regimenting the language syntax an the way this syntax is interpreted by the
browsers. Throughout the developers, there is a known exception Microsoft Internet Explorer which
it was always out of the standards in the past - this problem is fixed by Edge that is was released
starting with Windows 10.
JavaScript is a language build on the ‘single threaded’ model - because it was created for the GUI
purposes where the user interaction with the app interface is generating a big number of events that
have to be processed rapidly, one after one. This is contrary to the ‘multithreading’ concurrency,
specific to the server applications. This concurrency model is eliminating many possible concurrency
problems like ‘race conditions’. And because is based on an ‘event loop’ that is consuming events,
it is very simple to create new events - this is an explanation for the asynchronous nature of the
language.
In the last time, there is a tendency of using JavaScript not just on the client side but also on the
server side using technologies like Node JS (which is also used by Ionic). This is a reason for the
explosion of the JavaScript community. Also, JSON - which is, in fact, the data model from the
JavaScript language is a preferred choice in client-server communication (a replacement for XML)
- mainly when the client and the server are ‘speaking’ Javascript.
There are a series of popular JavaScript frameworks:
• JQuery - initially created for reducing the differences between the JavaScript implementations
on various Web browsers
• BlackboneJS, React.js, Vue.js, Angular 1/2 - MVC frameworks, letting us implement an
application completely and in a clean manner on the client side in an MVC style (taking with
the server side using REST API calls)
• UnderscoreJS, Lodash - simplify the work with JSON data
• RxJs - is short for Reactive Extensions for Javascript which brings the concept of Observables,
which we know and love a lot of a lot of server-side technologies, to Javascript world.
• TypeScript, Coffe Script - programming languages ‘transpiled’ to JavaScript
EcmaScript
The JavaScript language is specified by a standard named ECMAScript (European Computer
Manufacturers Association). The language used by all of our day’s browsers, named also regular
JavaScript is the version ES5 of this standard. Angular is based on TypeScript which has many
elements from the version ES6 of the standard (also named ES2015) which is coming with a set of
new concepts, not existing in ES5:
Introduction to Ionic 4 viii
• Classes – ES6 classes provide a way to encapsulate and extend the code.
• Modules – Provides a modular way of organizing and loading code.
• Arrow functions – A short-hand version of an anonymous function.
• Block-level scope – ES6 now supports scoping variables to blocks (if, for, while, etc.) using the
let keyword.
• Constants – We can now define constants in ES6 code using the const keyword.
• Destructuring – A succinct and flexible way to assign values from arrays or objects into
variables.
• Generators – Specialized functions that create iterators using function* and the yield keyword.
• Promises – Used with async operations.
• Rest parameters – Replaces the need for using arguments to access functions arguments. Allows
us to get to an array representing “the rest of the parameters”
• Default parameters – Ever wished that a function parameter could be assigned a default value?
We can do that now in ES6.
• Map – Dictionary type object that can be used to store key/value pairs.
• Set – A collection object that can be used to store a list of data values.
• Template Strings – Clean way to build up string values.
In the modern browsers, the support for JavaScript ES5¹² is excellent. JavaScript ES6¹³ is also well
supported in the modern browsers.
TypeScript
The Angular framework starting with version 2 is coming with the TypeScript language¹⁴ which
is adding data types to the classes and the new constructs taken from ES6¹⁵. TypeScript is very
similar with an object-oriented compiled language like Java - now when the source is edited (before
execution), type checks and static checks, in general, can be done - this is a significant advantage
because the number of possible runtime errors is decreased. There is excellent support in IDEs for
the Typescript language.
¹²http://kangax.github.io/compat-table/es5
¹³http://kangax.github.io/compat-table/es6
¹⁴https://www.infoq.com/articles/Angular2-TypeScript-High-Level-Overview
¹⁵http://kangax.github.io/compat-table/es6/#typescript
Introduction to Ionic 4 ix
Because TypeScript is not understood by the browsers, there is a process of transpiling. This is not
a compilation to an intermediate bytecode like in Java language, but a transformation of sources
from TypeScript to ES5, which is understood by all the browsers. This transformation it is done
automatically by Ionic in a dynamic way using the transpiler that is coming with the TypeScript
language (there are transpiler for the conversion from ES6 to ES5: Traceur¹⁶ by Google and Babel¹⁷
created by the JavaScript community)
it) or Kotlin, and we can develop using the Android SDK and the Android Studio IDE (based on
IntelliJ Idea). After building we will get a .apk file that can be published in Google Play store.
But if we want the same application in another app store, Apple App Store because we want to be
available on iPhones and iPads devices running the iOs operating system, we have to rewrite the app
in Objective-C or Swift and to use the Xcode mobile SDK and IDE. The same for Windows Mobile
which is based on C# and Visual Studio for development.
Cordova architecture
Advantages:
• cross-platform mobile development (we are in the same situation like Java: Write once, run
anywhere) we will develop an HTML5 application with very well known technologies like JS
(and JSON), CSS for powerful styling, HTML
• the developed app can also run in a desktop browser so we can deploy it as a classical Web
application and we can use PWA for encouraging mobile installs
• we can rely on the ability of the Web apps to be responsive²⁴ and to auto-adapt to the devices
resolutions
• because we have a web application, we can use the browser from to development environment
to test and debug it
Disadvantages:
• some very particular native functionalities are hard to be implemented compared with a native
application
²⁴https://developers.google.com/web/fundamentals/design-and-ui/responsive
Introduction to Ionic 4 xii
Android OS
Google is the owner of Android OS²⁷ and the devices are manufactured by many others like Samsung,
Motorola, etc. There is a reference device Google Pixel²⁸ series produced by Google. Till now it was
Nexus, but the manufacturer was not Google. The programming language is Java (Android is derived
from Java), and the development tool is Android Studio (based on IntelliJ Idea now, in the past was
Eclipse) which can run on Linux, Windows or Mac operation systems.
iOs OS
Apple is the owner of the iOs operating system²⁹ and is also the manufacturer of the iPhones³⁰ and
iPads³¹. The programming language in Objective C³² with the addition of a more scripting language
named Swift³³. The development tool is named XCode³⁴, and we need a Mac OS system to develop
our application.
²⁵http://www.idc.com/promo/smartphone-market-share/os
²⁶https://docs.microsoft.com/en-us/windows/uwp/get-started/whats-a-uwp
²⁷https://www.android.com
²⁸https://madeby.google.com/phone
²⁹http://www.apple.com/ios
³⁰http://www.apple.com/iphone
³¹http://www.apple.com/ipad
³²https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/
Introduction.html
³³https://developer.apple.com/swift/
³⁴https://developer.apple.com/xcode
Introduction to Ionic 4 xiii
Apple AppStore
Also, we have to upload the binary file, and this can be done in two ways. The first option is to
use Product/Archive option from Xcode. Second is using a distinct application named Application
Loader.