Now it’s Part-3 in Series of Ionic framework tutorials for Mobile App Development. In previous parts (Part-1 & Part-2) of this Ionic Tutorial Series, we covered the following topics. If you have reached to this page directly or not clear about above mentioned concepts, we’ll strongly recommend to go through previous Parts and read these for better understanding.
- Introduction to Ionic Framework
- Installation and Setup Development Environment
- Life-cycle of an Ionic Application
- How Navigation works?
- Caching View
- Publishing an Ionic Application
- User-defined Pipes
- Making an HTTP request from Ionic App
- Setting Up a test framework
Also, we have provided a separate Ionic Tutorial for Building a Hybrid Mobile Application Development using Ionic and Cordova; we will refer that already developed application throughout this article series to add more value by implementing more features.
- Ionic Tutorial for Mobile App Development - Part 1
- Ionic Tutorial for Mobile App Development - Part 2
- Build your first Hybrid Mobile App using Ionic and Cordova

Learning Ionic Mobile App Development
We are going to cover following topics in this Ionic Tutorial:
- How to use any device function from Ionic App? For example, how to use Camera from Ionic app?
- How to develop a directive using ionic2?
- How to use form validator to develop a form using ionic2?
- How to show toast in Ionic2 application?
- How to use Modal in Ionic 2?
- How to communicate from one view to another in Ionic applications with data?
- How to use SQLLite Ionic native Module in Ionic 2.0 for local storage?
- How to use Google API such as Geo location or google map in ionci2?
How to use any device function from Ionic App? For example, how to use Camera from Ionic app?
Usually, there are so many methods defined in ionic-native library to add any native functions.
For example, how to use Camera from Ionic app? Considering that we have an ionic application already developed as in another Ionic Tutorial here.
- Add Cordova camera plugin:
|
1 |
ionic plugin add cordova-plugin-camera |
|
1 2 3 4 5 |
<ion-content padding> <img [src]="encodedImage" *ngIf="encodedImage" /> <button ion-button secondary (click)="clickPicture()">Click</button> </ion-content> |
- Import Camera from Ionic Native:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import {Camera} from 'ionic-native'; Add a method clickPicture(){ Camera.getPicture({ destinationType: Camera.DestinationType.DATA_URL, targetWidth: 1000, targetHeight: 1000 }).then((imageData) => { // imageData is a base64 encoded string this.encodedImage = "data:image/jpeg;base64," + imageData; }, (err) => { console.log(err); }); } |
|
1 |
ionic run browser |
How to develop a directive using ionic 2?
Considering we already have developed an Ionic Mobile App, here we will develop a directive using Ionic 2 as follows:
Step 1: Run the following command from the command prompt in order to generate the directive:
|
1 |
ionic generate directive collapsableImage |
This command will generate the following files:
|
1 2 3 4 5 6 7 8 9 10 11 |
import { Directive } from '@angular/core'; @Directive({ selector: '[collapsable-image]' }) export class CollapsableImage { constructor() { console.log('This is CollapsableImage Directive'); } } |
Step 2: Declare the new directive in ngModule in app.module.ts
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@NgModule({ declarations: [ MyApp, Page1, CollapsableImage ], imports: [ IonicModule.forRoot(MyApp) ], bootstrap: [IonicApp], entryComponents: [ MyApp, Page1 ], providers: [] }) export class AppModule {} |
change CollapsableImage class as following:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
export class CollapsableImage { scrollerHandle: any; header: any; headerHeight: any; translateAmt: any; scaleAmt: any; scrollTop: any; lastScrollTop: any; ticking: any; constructor(public element: ElementRef, public renderer: Renderer) { } ngOnInit(){ this.scrollerHandle = this.element.nativeElement.getElementsByClassName('scroll-content')[0]; this.header = this.scrollerHandle.firstElementChild; this.headerHeight = this.scrollerHandle.clientHeight; this.ticking = false; this.renderer.setElementStyle(this.header, 'webkitTransformOrigin', 'center bottom'); window.addEventListener('resize', () => { this.headerHeight = this.scrollerHandle.clientHeight; }, false); this.scrollerHandle.addEventListener('scroll', () => { if(!this.ticking){ window.requestAnimationFrame(() => { this.updateElasticHeader(); }); } this.ticking = true; }); } updateCollapsableImage (){ this.scrollTop = this.scrollerHandle.scrollTop; if(this.scrollTop >= 0){ this.translateAmt = this.scrollTop / 2; this.scaleAmt = 1; } else { this.translateAmt = 0; this.scaleAmt = -this.scrollTop / this.headerHeight + 1; } this.renderer.setElementStyle(this.header, 'webkitTransform', 'translate3d(0,'+this.translateAmt+'px,0) scale('+this.scaleAmt+','+this.scaleAmt+')'); this.ticking = false; } |
How to use form validator to develop a form using ionic 2?
Using a form validator in an already developed Ionic 2 Mobile App, follow the steps:
Step 1: Create a Component for Form
Run the following command
|
1 |
ionic g page AddToDoPage |
Step 2: Update Component for Form
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import { FormBuilder, Validators, AbstractControl, ControlGroup } from 'angular2/common'; @Page({ templateUrl: 'build/pages/add_todo/ add_todo.html', }) export class AddToDoPage { static get parameters() { return [[FormBuilder]]; } constructor(formBuilder) { this.nav = nav; this.myData = null; this.myForm = formBuilder.group({ 'title': ['', Validators.required], ‘description’: ['', Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required])], ‘priority’: ['', PriorityValidator.isValid], ‘status’:[] }) this.subject = this.myForm.controls['subject']; this.message = this.myForm.controls['message'] } onSubmit(formData) { console.log('Form submitted is ', formData); this.myData = formData; } } |
Step 3: Create a user-defined form validator
It is also possible to create a user defined validator as following:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import { FormControl } from '@angular/forms'; export class PriorityValidator { static isValid(control: FormControl): any { if(isNaN(control.value)){ return { "not a number": true }; } if(control.value % 1 !== 0){ return { "not a whole number": true }; } if(control.value > 10 && control.value<1 ){ return { "invalid": true }; } return null; } } |
In this above example, a form builder has been used:
|
1 2 3 4 5 6 |
this.myForm = formBuilder.group({ 'title': ['', Validators.required], ‘description’: ['', Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required])], ‘priority’: ['', PriorityValidator.isValid], ‘status’:[] }) |
Among the form fields, ‘title’ has only one validation rule which is, this is required and then ‘description’ has more than one validation rule such as,
- description is a required field
- the maximum length of the field ‘description’ would not be more than 30
- and the text pattern for the description should be ‘[a-zA-Z ]*’ which means this is a alphabetic input
Similarly, priority from field also has its own priority However, PriorityValidator is a customized user defined validator.
And finally, status has no validation rule.
For these above flexibility form builder is highly recommended API in order to develop form.
How to show toast in Ionic 2 application?
Step 1: Create an Ionic App
Follow the steps described to develop an Ionic Application here and run the new ionic application on the browser.
Step 2: Create a Component
|
1 |
ionic generate page Page1 |
Step 3: add the following plugin
|
1 |
ionic plugin add cordova-plugin-x-toast |
Step 4: Update Page1
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import {Component} from '@angular/core'; import {NavController, Platform} from 'ionic-angular'; declare var window: any; @Component({ templateUrl: 'page1.html' }) export class Page1 { constructor(private navCtrl: NavController, private platform: Platform) { } showToast(message, position) { this.platform.ready().then(() => { window.plugins.toast.show(message, "short", position); }); } } |
Step 5: Change the Page1 HTML
Change Page1.html as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<ion-header> <ion-navbar> <ion-title> Home Page </ion-title> </ion-navbar> </ion-header> <ion-content padding> <button (click)="showToast('This toast would be seen on top of screen!', 'top')">Show at Top</button> <button (click)="showToast(' This toast would be seen on middle of screen!, 'center')">Show at Middle</button> <button (click)="showToast(' This toast would be seen on bottom of screen!', 'bottom')">Show at Bottom</button> </ion-content> |
How to use Modal in Ionic 2?
Step-1: Create an Ionic App
Follow the steps described in previously developed Ionic Application and run the new ionic application on the browser.
Step-2: Create a component
Run the following command
|
1 |
ionic generate page SimpleModalPage |
This will create the following class for the component or Page:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import {Page, ViewController} from 'ionic-angular'; @Page({ templateUrl: 'build/pages/simple-modal/sample-modal.html' }) export class SimpleModalPage { constructor(private viewCtrl: ViewController) { } dismiss(data) { this.viewCtrl.dismiss(data); } } |
Step-3: Add a template for component
|
1 2 3 4 5 6 7 8 9 10 11 |
<ion-navbar *navbar> <ion-buttons start> <button (click)="dismiss()"> Cancel </button> </ion-buttons> <ion-title>Simple Modal</ion-title> </ion-navbar> <ion-content padding class="simple-modal-page"> This is a simple doalogue </ion-content> |
Step-4: Trigger the modal from parent component
To run it from the home page, we need to run the following code:
|
1 2 3 4 5 6 7 8 9 |
export class HomePage { constructor(private nav: NavController) {} showModal() { const modal = Modal.create(SimpleModalPage); this.nav.present(modal); } } |
How to communicate from one view to another in Ionic applications with data?
Step-1: Update cordova
If cordova is not updated, it need to be updated with following command
|
1 |
npm update -g cordova ionic |
Step-2: Develop an App
Follow the instruction as given in another Ionic Tutorial to develop an Ionic app.
Step-3: add respective platform
For android, use the following command
|
1 |
ionic platform add android |
For iOS, use the following:
|
1 |
ionic platform add ios |
Step-4: Push information from Page1 To Page2
- In pages/page1/page1.ts file , import the following Navcontroller and Page 2
12import { NavController} from 'ionic-angular';import {Page2} from '../page2/page2'; - Declare the variable of NavController in constructor
123constructor(public navCtrl: NavController, _todoService:TodoService) {} - Push the variable to Page2
123456passTodo(){this.navCtrl.push(Page2, {tite: this.todo.title,description: this.todo.description});}
Step-5: Read information from Page2
- Import necessary libraries
1import {NavController, NavParams} from 'ionic-angular'; - Instantiate the variable in constructor
123constructor(private navController: NavController, private navParams: NavParams, private shareService: ShareService) {} - Read the parameter
12this.title = navParams.get('title');this.description = navParams.get(‘description’);
How to use SQLLite Ionic native Module in Ionic 2.0 for local storage?
Step-1: Install plugin
Use an existing ionic 2.0 application, or create an ionic 2.0 application following the steps described in another Ionic App development tutorial.
Then install SqlLite storage, running following command:
|
1 2 |
$ ionic plugin add cordova-sqlite-storage $ npm install --save @ionic-native/sqlite |
Step-2: Create a DB provider
Run the following command to generate a Provider:
|
1 |
ionic g provider DBProvider |
This command will generate a provider class name DBProvider inside provider folder.
There are 2 ways to connect with SQLite databases in Ionic 2, such as
- Using Ionic native’s SqLite adapter: provides nice transaction methods bridged directly of Cordova’s SQLite plugin
- Using Ionic’s SqlStorage Api: automatically configures SQLStorage in mobile and WebSQL in web browsers
Now in the DB provider we need different functionality for performing different option such as Create table, save data, retrieve data etc.
Here SQLite Ionic Native module will be used for this example, first import the necessary class in DbProvider.
|
1 |
Import { SQLite } from ‘ionic-native’; |
- CREATE DATABASE AND INITIALIZE THE VARIABLE
In the constructor of DBProvider:123456constructor(private _navController: NavController, private jsonp: Jsonp, private http: Http) {this.data = {};this.storage = new Storage(SqlStorage);.. … …} - CREATE A TABLE
12345678910111213141516platform.ready().then(() => {StatusBar.styleDefault();let db = new SQLite();db.openDatabase({name: "data.db",location: "default"}).then(() => {db.executeSql("CREATE TABLE IF NOT EXISTS profile (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, address TEXT)", {}).then((data) => {console.log(" profile TABLE CREATED: ", data);}, (error) => {console.error("Unable to execute sql", error);})}, (error) => {console.error("Unable to open database", error);});});
- SAVE BULK DATA
123456789saveProfiles = (profiles) => {let query = "INSERT OR REPLACE INTO profiles VALUES (?, ?, ?, ?, ?, ?)";for (let profile of profiles) {this.storage.executeSql(query, { profile.firstName,profile.lastName,profile.address });}return forecasts;}
- SAVE ONE DATA
123saveAddress(address: string) {this.storage.set('address', address);}
Step-3: Use DBProvider as a service in Other Page
- RETRIEVE DATA
12345678910getProfile(firstName: string) {return this.storage.executeSql("SELECT * FROM profiles WHERE firstName = ?", { firstName }).then((resp) => {if (resp.res.rows.length > 0) {for (var i = 0; i < resp.res.rows.length; i++) {let item = resp.res.rows.item(i);return item;}}});}
How to publish an Ionic Mobile Application?
Different Google APIs can be used for an Ionic Mobile Application as follows:
Google Analytics:
|
1 2 3 |
$ ionic plugin add cordova-plugin-google-analytics OR $ npm install --save @ionic-native/google-analytics |
Google Maps:
|
1 2 3 |
$ ionic plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE" OR $ npm install --save @ionic-native/google-maps |
Geo-location:
|
1 2 3 |
$ ionic plugin add cordova-plugin-geolocation OR $ npm install --save @ionic-native/geolocation |
Google Plus:
|
1 2 3 |
$ ionic plugin add cordova-plugin-googleplus --variable REVERSED_CLIENT_ID=myreversedclientid OR $ npm install --save @ionic-native/google-plus |
The course is designed to learn Building Native iOS and Android Applications using AngularJS, Cordova and the Ionic 2/3 Framework. Provide you the opportunity to Build 4 Complete Mobile Apps!
What you will learn in this Ionic Online Course:
- Using the powerful features of Ionic 2, how to Build Native Apps for iOS and Android with Angular.
- To get detailed and in-depth understanding of Ionic 2 to learn more about very advanced features and optimize Mobile Applications
- Testing on real devices for iOS and Android Mobile apps and how to publish those apps to the app stores of Android and Apple.
- View Complete Curriculum
Followings are few highlight about this Ionic 2/Ionic 3 Online Course:
- Best Seller Udemy Online Course
- 10,158++ students enrolled
- 1,795 Review available
- 14.5 Hours on-demand videos
- 26 Articles
- 25 Supplemental Resource
- Access on Mobile and TV
- Certificate of Completion
- Lifetime Access
- 30 days Money back guarantee
- You can get the course at Discounted Price here.
Most attractive feature is 30 days Money Back Guarantee means there is NO RISK. If you didn’t like this ASP.NET Core and Angular 2 Online Course, you can refund your money back within 30 days of purchase.

This was part-3 in Developing Mobile Application using Ionic Framework. You can go to Part-1 & Part-2 in this Ionic Tutorial Series for Mobile App Development from beginners to Intermediate level concepts and implementations. Keep in touch.





