3

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

  • Andreas Gassmann's answer work very well. For those who wonder how to access `window.plugins.{plugin-name}.{property-or-method}`, despite compilation errors : you can use `window['plugins']...` – Sylvain Martin Saint Léon Dec 15 '16 at 15:24

2 Answers2

8

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();
  }
}
Andreas Gassmann
  • 6,334
  • 7
  • 32
  • 45
  • This is what I was missing. I am not that familiar with typescript. I played with the plug-in in the `chrome://inspect console` and it didn't fit my needs. I am ending up writing it myself, so your insight is much appreciated, thanks. – Sylvain Martin Saint Léon Dec 12 '16 at 08:02
  • i have tried it the way specified above but still its not working can some one help me out. following is the issue: file:///android_asset/www/build/polyfills.js: Line 3 : Error: Uncaught (in promise): Error: Error in ./MyApp class MyApp - inline template:0:0 caused by: KioskPlugin is not defined ReferenceError: KioskPlugin is not defined – sakshi Dec 18 '16 at 18:52
  • Did you add `declare let KioskPlugin: any;` to the beginning of the MyApp class file? – Andreas Gassmann Dec 18 '16 at 21:06
  • 1
    i have done that still no change, why ionic 2 we is causing problems in integrating cordova plugins (non ionic native plugins) i am also working on other plugin integration https://www.npmjs.com/package/cordova-plugin-firebase even this is also not working. – sakshi Dec 20 '16 at 04:54
  • Did you solve this problem? I am investigating the same problem. – Alex Mar 03 '17 at 12:18
  • 1
    I am still at the same problem as well with the Uncaught(in promise) error message. – Patrick Odum Sep 13 '17 at 20:05
4

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 ;)

marcRDZ
  • 281
  • 2
  • 8