cordova-plugin-facebook v0.2.2
CordovaFacebook
Facebook Plugin for Cordova 5.0+
Supports:
- Cordova >= 5.0.0
- iOS and Android
- Login, Graph Requests, App Events
Get the Plugin:
$> cordova plugin add cordova-plugin-facebook --variable FACEBOOK_DISPLAY_NAME=<Your App Name> --variable FACEBOOK_APP_ID=<Your App ID> [--save]Using the Plugin:
CordovaFacebook defines a single variable on window: window.CordovaFacebook.
Callbacks:
All CordovaFacebook methods accept exactly one argument: options, of type Object.
Passing in a function as options.onSuccess or options.onFailure will allow you to listen to the result of that method.
Both onSuccess and onFailure callbacks will generally be passed one argument (whose type may vary) as the result.
CordovaFacebook.login
The CordovaFacebook.login method accepts a permissions field in addition to the standard callbacks.
permissions(optional) - an array of Facebook permissions you are asking for from the user."public_profile"is always requested. See Facebook's Docs for more information.
Both onSuccess and onFailure receive a single result object, with the following properties:
result.error-1if there was an error,0otherwise.result.success-1if the user accepted the login request,0otherwise.result.cancelled-1if the user cancelled the login request,0otherwise.result.errorCode(iferroris 1) - Facebook's error code for what went wrong.0is a network failure,304is a login mismatch (calllogoutbefore trying again). See Facebook List of Error Codes.result.errorLocalized(iferroris 1) - Facebook's localized description of what went wrong, in the current locale.result.granted(ifsuccessis 1) - An array of the permissions the user approved.result.declined(ifsuccessis 1) - An array of the permissions the user declined.result.accessToken(if available) - The Facebook Access Token for the User.result.userID(if available) - The Facebook User ID for the User.
Example usage:
CordovaFacebook.login({
permissions: ['email', 'user_likes'],
onSuccess: function(result) {
if(result.declined.length > 0) {
alert("The User declined something!");
}
/* ... */
},
onFailure: function(result) {
if(result.cancelled) {
alert("The user doesn't like my app");
} else if(result.error) {
alert("There was an error:" + result.errorLocalized);
}
}
});CordovaFacebook.logout
The CordovaFacebook.logout method does not have any additional options other than the standard callbacks.
Additionally, the logout method will always succeed, and never fail. (Meaning onFailure will never be called).
The onSuccess callback is not passed any arguments.
Example usage:
CordovaFacebook.logout({
onSuccess: function() {
alert("The user is now logged out");
}
});
// Unless you need to wait for the native sdk to do its thing,
// you dont even really need to use a callback:
CordovaFacebook.logout();