Search code examples
cordovaionic-frameworkionic-native

Can I install and use Ionic Native in a cordova project?


In this project I only use cordova 9 and angular 7 and not Ionic installed. But I want to use cordova plugins and I know Ionic Native wrap these plugins for use observes in angular Is mandatory install Ionic first?

In that case, must I unistall cordova or can live together.


Solution

  • Short answer Yes.

    As stated on their Website

    Ionic Native is a library of Cordova plugins and integrations that make it easy to add native functionality to any Ionic app, Cordova project, or WebView. Ionic Native is available in two editions: Community and Enterprise.

    Suppose you want to use ionic-native/Camera

    // app.module.ts

    import { Camera } from '@ionic-native/camera/ngx';
    
    ...
    @NgModule({
      ...
    
      providers: [
        ...
        Camera
        ...
      ]
      ...
    })
    export class AppModule { }
    

    After the plugin has been declared, it can be imported and injected like any other service:

    // camera.service.ts
    import { Injectable } from '@angular/core';
    import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
    
    @Injectable({
      providedIn: 'root'
    })
    export class PhotoService {
      constructor(private camera: Camera) { }
    
      takePicture() {
        const options: CameraOptions = {
          quality: 100,
          destinationType: this.camera.DestinationType.DATA_URL,
          encodingType: this.camera.EncodingType.JPEG,
          mediaType: this.camera.MediaType.PICTURE
        }
    
        this.camera.getPicture(options).then((imageData) => {
          // Do something with the new photo
    
        }, (err) => {
         // Handle error
         console.log("Camera issue: " + err);
        });
      }
    }