bajky
bajky

Reputation: 342

How to use cordova plugins in ionic 2

I'm new in ionic 2 and I use Typescript. I need use this pugin https://www.npmjs.com/package/cordova-plugin-nativestorage or something like this. How can I import this plugin ? How to use plugin, which is not ionic native ? It is possible ?

Upvotes: 4

Views: 4611

Answers (2)

Daniel Suchý
Daniel Suchý

Reputation: 1932

You can easily manage lot of Cordova plugins through Ionic Native that is automatically installed with Ionic 2.

For example if you want Cordova Plugin Native Storage, you will find this plugin in Ionic Native docs, here and as you can see there is all steps that you should do to made plugin work.

In our case you have to install plugin via console:

ionic plugin add cordova-plugin-nativestorage

Next import and use this plugin from Ionic Native in our app:

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.

Upvotes: 2

Tuong Le
Tuong Le

Reputation: 19220

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

ionic plugin cordova-plugin-nativestorage --save

Then, you can use the plugin as normal. Please check https://github.com/TheCocoaProject/cordova-plugin-nativestorage

Few things to check:

  • Make sure plugin has been installed. Please see the above command.
  • Test on real devices
  • Make sure your code is inside the deviceready event. document.addEventListener('deviceready', this.onDeviceReady, false);

Upvotes: 1

Related Questions