Facebook

Improve this doc

$ cordova plugin add cordova-plugin-facebook4 --variable APP_ID="123456789" --variable APP_NAME="myApplication"

Repo: https://github.com/jeduan/cordova-plugin-facebook4

Use the Facebook Connect plugin to obtain access to the native FB application on iOS and Android.

Requires Cordova plugin: cordova-plugin-facebook4. For more info, please see the Facebook Connect.

Installation

To use the FB plugin, you first have to create a new Facebook App inside of the Facebook developer portal at https://developers.facebook.com/apps.

fb-getstarted-1

Retrieve the App ID and App Name.

fb-getstarted-2

Then type in the following command in your Terminal, where APP_ID and APP_NAME are the values from the Facebook Developer portal.

cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication"

After, you'll need to add the native platforms you'll be using to your app in the Facebook Developer portal under your app's Settings:

fb-getstarted-3

Click 'Add Platform'.

fb-getstarted-4

At this point you'll need to open your project's config.xml file, found in the root directory of your project.

Take note of the id for the next step:

<widget id="com.mycompany.testapp" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">

You can also edit the id to whatever you'd like it to be.

iOS Install

Under 'Bundle ID', add the id from your config.xml file:

fb-getstarted-5

Android Install

Under 'Google Play Package Name', add the id from your config.xml file:

fb-getstarted-6

And that's it! You can now make calls to Facebook using the plugin.

Events

App events allow you to understand the makeup of users engaging with your app, measure the performance of your Facebook mobile app ads, and reach specific sets of your users with Facebook mobile app ads.

Activation events are automatically tracked for you in the plugin.

Events are listed on the insights page.

For tracking events, see logEvent and logPurchase.

Usage

import {Facebook} from 'ionic-native';

Static Methods

login(permissions)

Login to Facebook to authenticate this app.

{
  status: "connected",
  authResponse: {
    session_key: true,
    accessToken: "kgkh3g42kh4g23kh4g2kh34g2kg4k2h4gkh3g4k2h4gk23h4gk2h34gk234gk2h34AndSoOn",
    expiresIn: 5183979,
    sig: "...",
    secret: "...",
    userID: "634565435"
  }
}
Param Type Details
permissions string[]

List of permissions this app has upon logging in.

Returns: Returns a Promise that resolves with a status object if login succeeds, and rejects if login fails.

logout()

Logout of Facebook.

For more info see the Facebook docs

Returns: Returns a Promise that resolves on a successful logout, and rejects if logout fails.

getLoginStatus()

Determine if a user is logged in to Facebook and has authenticated your app. There are three possible states for a user:

1) the user is logged into Facebook and has authenticated your application (connected) 2) the user is logged into Facebook but has not authenticated your application (not_authorized) 3) the user is either not logged into Facebook or explicitly logged out of your application so it doesn’t attempt to connect to Facebook and thus, we don’t know if they’ve authenticated your application or not (unknown)

Resolves with a response like:

{
  authResponse: {
    userID: "12345678912345",
    accessToken: "kgkh3g42kh4g23kh4g2kh34g2kg4k2h4gkh3g4k2h4gk23h4gk2h34gk234gk2h34AndSoOn",
    session_Key: true,
    expiresIn: "5183738",
    sig: "..."
  },
  status: "connected"
}

For more information see the Facebook docs

Returns: Returns a Promise that resolves with a status, or rejects with an error

getAccessToken()

Get a Facebook access token for using Facebook services.

Returns: Returns a Promise that resolves with an access token, or rejects with an error

showDialog()

Show one of various Facebook dialogs. Example of options for a Share dialog:

{
  method: "share",
  href: "http://example.com",
  caption: "Such caption, very feed.",
  description: "Much description",
  picture: 'http://example.com/image.png'
}

For more options see the Cordova plugin docs and the Facebook docs

Returns: Returns a Promise that resolves with success data, or rejects with an error

api(requestPath, permissions)

Make a call to Facebook Graph API. Can take additional permissions beyond those granted on login.

For more information see:

Calling the Graph API - https://developers.facebook.com/docs/javascript/reference/FB.api Graph Explorer - https://developers.facebook.com/tools/explorer Graph API - https://developers.facebook.com/docs/graph-api

Param Type Details
requestPath string

Graph API endpoint you want to call

permissions string[]

List of permissions for this request.

Returns: Returns a Promise that resolves with the result of the request, or rejects with an error

logEvent(name, params, valueToSum)

Log an event. For more information see the Events section above.

Param Type Details
name string

Name of the event

params Object

An object containing extra data to log with the event

valueToSum number

any value to be added to added to a sum on each event

Returns:

logPurchase(value, currency)

Log a purchase. For more information see the Events section above.

Param Type Details
value number

Value of the purchase.

currency string

The currency, as an ISO 4217 currency code

Returns: Returns a Promise

appInvite(options)

Open App Invite dialog. Does not require login.

For more information see:

the App Invites Overview - https://developers.facebook.com/docs/app-invites/overview the App Links docs - https://developers.facebook.com/docs/applinks

Param Type Details
options Object

An object containing an App Link URL to your app and an optional image URL. url: App Link to your app picture: image to be displayed in the App Invite dialog

Returns: Returns a Promise that resolves with the result data, or rejects with an error

API

Native

General