Hello Readers,
In today’s post will see how to use Facebook SDK in Cordova app. First of all we need to register our app with Facebook and have an App Id.
If you have not registered your app with Facebook you can Click here and follow the easy steps. In Cordova, we can use Facebook SDK easily with the Cordova Facebook plugin. To install the plugin we need an App_Id which generates when we register app with Facebook.
Now install the plugin through CLI :
$ cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication"
In the above command, we need to put App_Id in place of "123456789" and App_Name in place of "myApplication".
This plugin compatible with Cordova >= 5.0, cordova-android >= 4.0, cordova-ios >= 3.8
For iOS App: You need to ensure that you've proper setup for the native iOS app in Facebook app dashboard.
For Android App: As iOS app setup, you've to properly setup for Android app with Facebook app dashboard and you need to generate a hash key of your android and submit it on the Facebook developer page where you created the android app.
Now we need Facebook SDK as a library of your project because in Android project file Connectplugin.java which contains error if we don't add Facebook SDK as library. To import Facebook SDK in eclipse you can visit: Click here.
Now we have done all necessary things. Now move to the further functionality:
This plugin creates an object facebookConnectPlugin with some methods which are as follows:
Login: User can log in into the Facebook app.
facebookConnectPlugin.login(["public_profile"], LoginSuccess,
function loginError (error) {
console.error(error)
}
);
var LoginSuccess = function (userData) {
console.log("UserInfo: ", userData);
}
Access Token: If you need access token then you can get it getAccessToken method.
facebookConnectPlugin.getAccessToken(function(token) {
console.log("Token: " + token);
});
Login Status: User can get login status from getLoginStatus method.
facebookConnectPlugin.getLoginStatus(function onLoginStatus (status) {
console.log("current status: ", status);
});
Post or share on wall: Use can share content on his wall. User can share content on wall with showDialog method.
facebookConnectPlugin.showDialog({
method: "share"
}, function onShareSuccess (result) {
console.log("Posted. ", result);
});
So Facebook4 plugin is very useful and we can use the Facebook functionality easily.
For more information about the plugin visit: Cordova-Facebook4-plugin. Hope this will help you :)
0 Comment(s)