RSS

Tag Archives: mobile

Integrate Ionic in Existing Angular Project

Angular Project

I just finished my project, written in Angular and although it runs on mobile browsers, it needs to be deployed as a native application. The choice is obvious, Ionic.

I’m integrating Ionic for Android development, but it’s optional. You don’t have to prepare your Ionic project for Android development, if you don’t want to. The same concept can also be used to prepare your project for iOS development.

This applies to:

  • Angular 7.
  • Ionic 5.
  • Java SDK 8 update 202.
  • Android SDK 9.0 (API level 28)
  • Android Studio 3.3.2

Here’s my approach integrating Ionic in my Angular project.

Bring-in Ionic

The step includes creating a new project using Ionic CLI, I’m going to refer it as Ionic project while my original Angular project as, well, Angular project.

  • In your Angular project, update all npm packages, to the latest major if you can. This is to avoid version conflict with Ionic project’s npm packages.
  • Start a new Ionic blank project.
    ionic start project-name blank
    
  • Update all npm packages in the newly created Ionic project.
    npm i -g npm-check-updates && ncu -u
    
  • Copy ionic.config.json from Ionic project to Angular project.
  • Copy angular.json from Ionic project to Angular project.
    • If you have specifically changed anything in your angular.json, make that necessary changes in the Angular project after the copy.
    • Ionic uses SCSS for style sheet by default, so if you are not using SCSS, make sure to copy settings under projects/app/architect/**/options/styles from Angular project’s angular.json prior to copy.
    • integrate-ionic-in-angular-project-1
  • Copy package.json from Ionic project to Angular project.
      • If you have specifically changed anything in your package.json (npm scripts, etc), make that necessary changes in the Angular project after the copy.
      • Combine the npm packages from both projects setting under dependencies and devDependencies.
      • integrate-ionic-in-angular-project-2

     

  • Combine .gitignore files from both projects, if you are using Git source control.
  • In Angular project, delete package-lock.json file and node_modules folder. These should be located in root of the project.
  • In Angular project, run npm install command.
    npm install
    
  • Test and make sure everything runs.
    // Test run with Ionic
    ionic serve
    // Test run with Angular
    ng serve --open
    
  • If you want to prepare your project for Cordova, runs the following command. Note that environment setup is required. Please refer to reference section for more details.
    // For Android
    ionic cordova prepare android
    // For iOS
    //ionic cordova prepare ios
    

References

Advertisements
 
Leave a comment

Posted by on March 11, 2019 in General

 

Tags: , , , , , , ,

Ionic 3/Angular 5 – Enable Swipe All Directions

By default Ionic 3 gestures only allow left and right swipe. Follow this step to enable all directions swipe, including up and down.
This will work on Angular application as well.

1. Install HammerJS
npm install –save hammerjs

2. Create new HammerJS config
Create new class (Typescript file). The class should have following definition:

import * as Hammer from 'hammerjs';
import {HammerGestureConfig} from '@angular/platform-browser';

export class MyHammerConfig extends HammerGestureConfig {
  overrides = {
    'swipe': {direction: Hammer.DIRECTION_ALL}
  }
}

3. Register new HammerJS config
In app.module.ts, register HammerJS config.
Most important lines are the import at the beginning, which import new HammerJS config class and register in providers section of @NgModule.

import {MyHammerConfig} from '../HammerConfig'; // Add this line

@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    {provide: HAMMER_GESTURE_CONFIG, useClass: MyHammerConfig} // Add this line
  ]
})
export class AppModule {
}

4. Use Ionic gestures as usual, see https://ionicframework.com/docs/components/#gestures, now Ionic should be smart enough to detect swipe up and down events.

Swipe up and swipe down event through Chrome’s developer tools.
Image 2018-06-20 at 12.03.50 AMImage 2018-06-20 at 12.04.26 AM

 
Leave a comment

Posted by on June 19, 2018 in General

 

Tags: , , , , , , , ,

Ionic ion-nav RootPage’s ionViewDidLoad() Does Not Fire

Ionic navigation provide [root] attribute to set the root page.

MyApp component:

<ion-nav [root]="rootPage"></ion-nav>
export class MyApp {
rootPage: any = StartPage; // This set StartPage as the root page of our app

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
    });
  }
}

And the StartPage

export class StartPage {
  constructor(private navCtrl: NavController,
              private modalController: ModalController) {
  }

  ionViewDidLoad() {
    console.log('StartPage ionViewDidLoad'); // This does not fire!
  }
}

All works well, except our ionViewDidLoad() in StartPage does not fire!

This is because when we set rootPage properties or present a page with ModalController, class’s constructor and ionViewDidLoad() do not get executed.

It seems like a bug in Ionic framework. For the record this was tested with IOS Simulator on:

  • Ionic Framework 3.8.0
  • Ionic CLI 3.2.0
  • Cordova CLI 7.1.0
  • NPM 3.10.10
  • Node 6.10.3
  • ios-deploy 1.9.2

The Workaround

Insert new page in between MyApp and StartPage, we will call it TransitionPage. Then explicitly set StartPage as the root of NavController.

Continue the code above. This is how modified MyApp look like:

<ion-nav [root]="rootPage"></ion-nav>
export class MyApp {
  rootPage: any = TransitionPage; // Our new transition page

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
    });
  }
}

This is our new TransitionPage:

export class TransitionPage {

  constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.navCtrl.setRoot(StartPage); // Manually set StartPage as the root
  }
}

The StartPage does not change, however its ionViewDidLoad() hook now fire as expected.

 

 
Leave a comment

Posted by on May 5, 2018 in General

 

Tags: , , , , , ,

 
%d bloggers like this: