How to use non native cordova plugins in ionic 2

I am trying to use this cordova plugin https://github.com/honza889/cordova-plugin-kiosk in an Ionic v2 App.

Basically after installing the plugin in cordova with cordova plugin add https://github.com/honza889/cordova-plugin-kiosk.git the plugin is enabled and working.

But I can't find a way to use the plugin's methods.

window.plugins is undefined

and cordova is undefined.

I have tried to import the plugin in app.component.ts but I can't figure out the right path.

Does anyone know a way to use non native plugins in an Ionic V2 app (the documentation and resources are outdated or referring to native plugins).

Thanks in advance

Upvotes: 3

Views: 3854

Answers (2)

marcRDZ
marcRDZ

Reputation: 301

I had a similar issue trying to access to a custom plugin and I finally solved thanks to this post: https://github.com/ionic-team/ionic-native/issues/525

Adding declare var cordova: any; instead of my custom plugin var, and calling through cordova.plugins.myCustomPlugin.myFunction() did the trick.

Indeed, depends on the settings in the plugin.xml file of the plugin you want to use. More concretely on the <clobbers target="variable.MyCustomPlugin" /> tag, which determines where the plugin js files will be exported. Another posibility for example would be declare var window: any; and then window.myCustomPlugin.myFunction()

Hope it helps ;)

Upvotes: 4

Andreas Gassmann
Andreas Gassmann

Reputation: 6544

You should be able to simply access the KioskPlugin variable in your code. The typescript compiler will not know that variable, so you have to declare it first:

declare let KioskPlugin: any;

@Component({
  ...
})
export class TestPage {

  ...

  exitKiosk() {
    KioskPlugin.exitKiosk();
  }
}

Upvotes: 8

Related Questions