Use custom cordova plugin in Ionic 2 : [ios filepicker and ...]


#1

hi

how can i use custom cordova plugin in ionic 2 ?
we cant import custom plugin in our project and using (window) get us errors.

ionic plugin add https://github.com/jcesarmobile/FilePicker-Phonegap-iOS-Plugin.git

i want to create some cordova plugin for my ionic 2 app and i don’t know how can i use them in my ionic 2 project.

did anyone experience that?

thanks


#2
  1. You don’t need import, just install plugin with npm pluginName --save. For normal typescript compilation - declare your plugin variable at top of your file. For example you need cordova
declare let cordova: any;
  1. Plugins work only on real devices and emulators. Launch with ionic serve will cause an error. So you can try livereload on devices. Something like:
ionic emulate ios -a -b -s -c -l

And see console.log in your terminal.

  1. Also you can try run your app on browser platform, but in my experience a half of plugin not work on it:
ionic run browser

#3

thanks snikh

i run my app on ios emulator

ionic emulate ios

and i don’t use serve and browser version.

for example in filepicker plugin

we have this in plugin docs.

FilePicker.isAvailable(function (avail) {
alert(avail ? “YES” : “NO”);
});

but in ionic 2 , we can’t use this or import custom plugin.

i used these codes and they didn’t work

cordova.plugins.FilePicker.isAvailable(function (avail) {
alert(avail ? “YES” : “NO”);
});

** i declared cordova too.

(window).plugins.FilePicker.isAvailable(function (avail) {
alert(avail ? “YES” : “NO”);
});

and i used this article too.

but…

ANY IDEAS ?


#4

I don’t work with phonegap plugins, but try this:

declare let FilePicker: any; // at the top of your file

// in your class method
FilePicker.isAvailable(function (avail) {
  alert(avail ? "YES" : "NO");
});

If it don’t work - show your console.log here.


#5

this 2 codes work fine

yours :

declare let FilePicker: any; // at the top of your file

// in your class method
FilePicker.isAvailable(function (avail) {
alert(avail ? “YES” : “NO”);
});

without declare :

(<any>window).FilePicker.isAvailable(function (avail) {
alert(avail ? “YES” : “NO”);
});

thanks a lot .