Cordova Plugins Syntax in Ionic 2


#1

Hey Guys,
I did a lot of googling and couldn’t seem to come up with much of an answer, how does the syntax of calling Cordova plugins in Ionic 2 work.

Like for example, in Ionic 1:
I was using a facebook plugin, and I would call it like: $cordovaFacebook.login([“public_profile”, “email”,“name”,“last_name”,“first_name”,“birthday”,“age_range”,“link”]).then(function (success) {

I would also ‘inject’ it and add it as a dependency. Both things I am not sure how to do with Ionic 2.

I read somewhere that with Ionic 2 that isn’t as nesscary, but I am still unsure


#2

The Ionic team are working on something called “Ionic Native” which will likely implement Cordova plugins in a similar way to the way you are used to using plugins in Ionic 1.

For now though, you just have to use the standard Cordova plugin syntax. Check the documentation for the plugins on GitHub, but in general you install the plugin with the usual command and then you will be able to access a global object that provides the functionality of the plugin. No need to inject it anywhere.


#3

Thanks for your reply! Love your website by the way…

What is the global object? or how do you set it? Sorry just have never used cordova plugins before in Ionic 2…

like for cordovaFacebook, would that be the default global object? Or is it somehow set?


#4

It’s important to note that the current syntax isn’t “using cordova plugins in Ionic 2” it’s just “using Cordova plugins”. ngCordova was a “special” way for Ionic 1, so there is no such thing as cordovaFacebook in Ionic 2 because that was a part of ngCordova.

There’s a few Facebook plugins out there, but this is a good one:

So in the case of this plugin, after you install it in your project you will have access to the facebookConnectPlugin object which will be available globally - you don’t need to inject it anywhere it will just be available.

Then you just follow the documentation for the plugin on how to use it, i.e:

facebookConnectPlugin.api(etc)

facebookConnectPlugin.login(etc)

If you were using some other Cordova plugin like Super Cool Plugin then you would probably do something like:

superCoolPlugin.someMethod()

Not all Cordova plugins work this way, but most do.


#5

ahh okay, Thanks for your help… just one more question if you dont mind…

if I have a class… or a page:
export class Page2 {
constructor() {

	function joe()
	{
		console.log("Jono");
	}

}
Login(){
console.log(“Login Click”);
}

}

Is there anyway to call the Login method in the constructor… tried the obvious 'Login()" but it just threw errors


#6

You can call any method in your class from your constructor (or from any method in the class) using:

this.myMethod();

this is like $scope from Ionic 1. Also, you should name your function login() not Login(), it’ll still work but typically capitals are used for class names and camel case is used for functions. Don’t know if the formatting is just messed up but not sure what you’re doing with your joe function, that probably shouldn’t be there.


#7

Hello! I’m trying to use that plugin with no luck: after install it I couldn’t get access to facebookConnectPlugin.

Am I missing something? the docs only ask for running cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication"