Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building a Mobile App with Angular 2 and Ionic 2

Building a Mobile App with Angular 2 and Ionic 2

How to build hybrid mobile apps with angular 2 and ionic 2

Avatar for Esinniobiwa Quareeb

Esinniobiwa Quareeb

February 18, 2017
Tweet

More Decks by Esinniobiwa Quareeb

Other Decks in Programming

Transcript

  1. What I do FullStack Developer, Rockstar Trainer, Technical Writer and

    Aspiring Evangelist • Founder & Mobile Apps Developer at Devplus Evolution • Web Application Developer at Qingdom Technologies and Paramount Computers • Rockstar Trainer & Instructor at FoundersHub and Devplus #CodeSquad
  2. WHAT YOU SHOULD ALREADY KNOW HTML CSS JAVASCRIPT Requirement Passion

    and Fuel (e.g Coffee) Yea, It is as simple as that CodeSquad
  3. Mobile Apps A mobile app is a software application designed

    to run on mobile devices such as smartphones and tablet computers. Most such devices are sold with several apps bundled as pre-installed software, such as a web browser, email client, calendar, mapping program, and an app for buying music or other media or more apps. Types of Mobile Apps • Native Apps • Web Apps • Hybrid Apps
  4. Hybid Mobile Apps Hybrid apps are part native apps, part

    web apps. (Because of that, many people incorrectly call them “web apps”). Like native apps, they live in an app store and can take advantage of the many device features available. Like web apps, they rely on HTML being rendered in a browser, with the caveat that the browser is embedded within the app.
  5. Often, companies build hybrid apps as wrappers for an existing

    web page; in that way, they hope to get a presence in the app store, without spending significant effort for developing a different app. Hybrid apps are also popular because they allow crossplatform development and thus significantly reduce development costs: that is, the same HTML code components can be reused on different mobile operating systems. Tools such as PhoneGap and Sencha Touch allow people to design and code across platforms, using the power of HTML.
  6. Things to consider in choosing the choice of mobile apps

    type are: • Speed • Device Accessibility • User Experience • Cost • Development Effort
  7. Think about speed, development time, cost and other constraint then

    Think of IONIC IONIC 1 VS ANGULAR 1 IONIC 2 VS ANGULAR 2
  8. Let’s Jumpstart developing our first Application Installing ionic Installing Node

    and NPM Generating first project Adding Platforms Running Application Updating the Application Understanding file structure Quick look into typescript
  9. • Installing Ionic Ionic 2 apps are created and developed

    primarily through the Ionic command line utility (the “CLI”), and use Cordova to build and deploy as a native app. This means we need to install a few utilities to get developing. • Ionic CLI and Cordova To create Ionic 2 projects, you’ll need to install the latest version of the CLI and Cordova. Before you do that, you’ll need a recent version of Node.js. Download the installer for Node.js 6 or greater and then proceed to install the Ionic CLI and Cordova for native app development: npm install -g ionic cordova
  10. This command will install the Ionic 2 CLI (Command Line

    Interface), which will allow you to generate Ionic 2 applications. It also installs Cordova which is a wrapper that allows you to access native functionality, and submit your application to app stores. It's important to note that if you have existing Ionic 1 projects this will make no difference. You can continue working on your Ionic 1 projects as per normal, even with the Ionic 2 CLI installed. Running the following command: ionic start MyApp blank Will just generate an Ionic 1 application. In order to generate an Ionic 2 application, you must provide the -- v2 flag: ionic start MyApp blank --v2
  11. Understanding file structure • If you take a look at

    the files and folders generated, it all looks pretty similar to an Ionic 1 application initially. We have a pretty typical Cordova style project structure. • If you look in the src folder though (which is where most of your development will occur), things start looking a little bit different:
  12. Index.html As is always the case, the first thing that

    gets opened by the browser is the index.html file.
  13. assets The assets folder can be used to store any

    static files you want to include in your project like images, or JSON data files. Anything in this folder will be copied over to your build folder each time the application is built.
  14. theme The theme folder contains the global.scss and variables.scss files

    for your applications. Most of the styling in your application will be done by using each components own .scss file, but you can use the global.scss file to define any custom styles, and you can modify the SASS variables in the variables.scss file to modify the style of your application in various ways.
  15. app All Ionic 2 apps will have a root component.

    This is not all that different to all the other components in your application, one obvious difference you will notice though is that it is in its own app folder, and is named app.component.ts.
  16. Decorators Decorators are extremely important to understand. They are the

    weird @Something things that sit above your class definitions. Essentially, they allow you to add some meta data to your class. They look a little strange at first, but they are quite simple once you wrap your head around it.
  17. Navigation I don't think I need to explain why navigation

    is important - it's how you can get from one page to another in your application. Navigation works quite differently in Ionic 2, adopting a native style stack navigation, where you can push and pop views.
  18. Styling & Themeing The default styling for Ionic 2 components

    is pretty sleek, but you probably don't want to release an app to the app store like that. You will at least want your own colours and branding, and you may even want to take the theme even further to enhance the users experience. There's a bunch of different ways you can alter your applications theme with Ionic 2, so it's important you understand the different methods.
  19. Fetching Remote Data Fetching data from a remote source is

    such a common feature I think it's important to cover it. In Ionic 2, the Http service is used to fetch data from a URL.
  20. Native Functionality One of the coolest things about Ionic apps

    is that it is super easy to incorporate native functionality like the devices Camera. To make it even easier in Ionic 2, the Ionic team have created Ionic Native.
  21. In this codelab you will learn how to: • Start

    a blank Ionic 2 app • Add pages using the CLI • Styling the components of your app • Add a service to structure your code • Implement a simple navigation flow Sounds like a lot? • Well it is, but you want to learn the basics of Ionic 2 so it’s best to just dive right into the fun!
  22. Setting up a new Ionic 2 app • As always

    we start with a blank new app and use the Ionic generator command to add 2 pages and a service. Otherwise we don’t need any additional stuff, so go ahead an run: ionic start ionic2-simpleLogin blank --v2 cd ionic2-simpleLogin ionic g provider authService ionic g page register ionic g page login
  23. This will setup all the needed files in the right

    places. Currently (version RC1) we have to hook up everything inside the src/app/app.module.ts so open the file and replace everything with: import { NgModule } from '@angular/core'; import { IonicApp, IonicModule } from 'ionic-angular'; import { MyApp } from './app.component'; import { HomePage } from '../pages/home/home'; import { LoginPage } from '../pages/login/login'; import { AuthService } from '../providers/auth-service'; import { RegisterPage } from '../pages/register/register'; @NgModule({ declarations: [ MyApp, HomePage, LoginPage, RegisterPage ], imports: [ IonicModule.forRoot(MyApp) ], bootstrap: [IonicApp], entryComponents: [ MyApp, HomePage, LoginPage, RegisterPage ], providers: [AuthService] }) export class AppModule {}
  24. We need to add our pages to the declarations and

    entryComponents and the service we created to the array of providers. Finally we need to tell our app to start with our generated LoginPage instead of the default home me page, so open the src/app/app.component.ts and change it to: import { Component } from '@angular/core'; import { Platform } from 'ionic-angular'; import { StatusBar } from 'ionic-native'; import { LoginPage } from '../pages/login/login'; @Component({ template: `<ion-nav [root]="rootPage"></ion-nav>` }) export class MyApp { rootPage = LoginPage; constructor(platform: Platform) { platform.ready().then(() => { StatusBar.styleDefault(); }); } }
  25. Creating the Authentication service • We use a service to

    handle the login, register and handling the current user. This service will use hard coded values so you would have to fit in your backend calls at the correct positions. • We also define a very simple User object which consists of a name and email. It’s easier to work with these classes than always using some object with undefined values, so embrace TypeScript and use classes. • Open the src/providers/auth-service.ts and insert:
  26. import { Injectable } from '@angular/core'; import {Observable} from 'rxjs/Observable';

    import 'rxjs/add/operator/map'; export class User { name: string; email: string; constructor(name: string, email: string) { this.name = name; this.email = email; } } @Injectable() export class AuthService { currentUser: User; public login(credentials) { if (credentials.email === null || credentials.password === null) { return Observable.throw("Please insert credentials"); } else { return Observable.create(observer => { // At this point make a request to your backend to make a real check! let access = (credentials.password === "pass" && credentials.email === "email"); this.currentUser = new User('Simon', '[email protected]'); observer.next(access); observer.complete(); }); } } public register(credentials) { if (credentials.email === null || credentials.password === null) { return Observable.throw("Please insert credentials"); } else { // At this point store the credentials to your backend! return Observable.create(observer => { observer.next(true); observer.complete(); }); } } public getUserInfo() : User { return this.currentUser; } public logout() { return Observable.create(observer => { this.currentUser = null; observer.next(true); observer.complete(); }); } }
  27. To cut the long story short Let’s go to our

    Program Code to understand the rest of the codelab Just sit back and watch
  28. Conclusion • In this codelab we have created a very

    basic Ionic 2 login template which can be the base for your next project. Of course you need to plugin your real backend at the correct spots, so this is only the start of your app. • Recall, http is use to fetch data from remote server