How to use cordova plugins in ionic 2

I am new to ionic 2 and I am using Typescript. I need to use this pugin https://www.npmjs.com/package/cordova-plugin-nativestorage or something like this. How to import this plugin? How to use a plugin that is not ionic native? Is it possible?

+4
source share
2 answers

You can easily control many Cordova plugins through Ionic Native , which is automatically installed using Ionic 2.

For example, if you want the Cordova Plugin Native Storage plugin, you will find this plugin in the Ionic Native docs, here , and, as you can see, there are all the steps you need to take to get the plugin to work.

In our case, you need to install the plugin through the console:

ionic plugin add cordova-plugin-nativestorage

Then import and use this plugin from Ionic Native in our application:

import {NativeStorage} from 'ionic-native';

NativeStorage.setItem('myitem', {property: 'value', anotherProperty: 'anotherValue'})
  .then(
    () => console.log('Stored item!'),
    error => console.error('Error storing item', error)
  );

Enjoy.

+2
source

First you need to install the plugin using the following command:

ionic plugin cordova-plugin-nativestorage --save

Then you can use the plugin as usual. Check out https://github.com/TheCocoaProject/cordova-plugin-nativestorage

A few things to check:

  • Make sure the plugin is installed. See the above command.
  • Testing on real devices
  • , deviceready . document.addEventListener('deviceready', this.onDeviceReady, false);
+1

All Articles