This is the official plugin for Facebook in Apache Cordova/PhoneGap!
The Facebook plugin for Apache Cordova allows you to use the same JavaScript code in your Cordova application as you use in your web application. However, unlike in the browser, the Cordova application will use the native Facebook app to perform Single Sign On for the user. If this is not possible then the sign on will degrade gracefully using the standard dialog based authentication.
- Supported on PhoneGap (Cordova) v2.1.0 and above.
The Facebook SDK (both native and JavaScript) is changing independent of this plugin. The manual install instructions include how to get the latest SDK for use in your project.
To use this plugin you will need to make sure you've registered your Facebook app with Facebook and have an APP_ID (https://developers.facebook.com/apps).
If you plan on rolling this out on iOS, please note that you will need to ensure that you have properly set up your Native iOS App settings on the Facebook App Dashboard. Please see the Getting Started with the Facebook SDK: Create a Facebook App section, for more details on this.
If you plan on rolling this out on Android, please note that you will need to generate a hash of your Android key(s) and submit those to the Developers page on Facebook to get it working. Furthermore, if you are generating this hash on Windows (specifically 64 bit versions), please use version 0.9.8e or 0.9.8d of OpenSSL for Windows and not 0.9.8k. Big ups to fernandomatos for pointing this out!
|_example | |_Simple | | |_index.html | |_HackBook | |_index.html | |_README | |_hackbook.manifest | |_img | |_css | |_js |_test | |_pg-plugin-fb-connect-tests.js |_src | |_android | | |_ConnectPlugin.java | | |_facebook | |_ios | |_FacebookConnectPlugin.m | |_FacebookConnectPlugin.h | |_facebook |_www | |_cdv-plugin-fb-connect.js | |_facebook_js_sdk.js
www/facebook_js_sdk.js
is the modified facebook-js-sdk. It already includes the hooks to work with this plugin.
www/cdv-plugin-fb-connect.js
is the JavaScript code for the plugin, this defines the JS API.
src/android
and src/ios
contain the native code for the plugin for both Android and iOS platforms. They also include versions of the Android and iOS Facebook SDKs. These are used during automatic installation. During manual installation, you are encouraged to download the most recent versions of the Facebook SDKs for you projects.
If you are using this plugin on Adobe PhoneGap Build, make sure you have done all the things on this checklist:
- Create a facebook app (see above)
- Set the facebook app sandbox mode to "disabled"
- In the facebook app settings, set the Site URL (under Website with Facebook Login) to
http://localhost
- If you plan on deploying the Android package, you must also enable this in the facebook app's settings: under "Native Android App" set the "package name" to the "widget" id attribute as specified in your phonegap/cordova config.xml, in the form
com.aaa.bbb
, also set the "class name" to the same value as the "package name" appending ".ProjectActivity". For example,com.aaa.bbb.ProjectActivity
, and enable the Facebook Login option. Finally you will need to generate an Android key if you don't already have one, and the corresponding facebook key hash from the Android keystore. See more info [here] (https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/). - In PhoneGap Build, make sure your Android key is added to your application, and that it is unlocked. You will need the passwords and the alias you used while creating the keystore in order to unlock. See more info [here] (https://build.phonegap.com/docs/config-signing-android).
Additional PhoneGap Build documentation available [here] (https://build.phonegap.com/docs/plugins#facebookconnect).
- NOTE: Min Target has to be set to 8. The plugin has an issue if you set your minimum target higher than that. You can edit this in your Android Manifest file. Set the Project Build Target to at least 11 if you see Android Manifest errors related to newer attributes that have been added in 2.2.0.
-
In the Cordova Android application you will need to put the following in your
res/xml/config.xml
file as a child to the plugin tag:<plugin name="org.apache.cordova.facebook.Connect" value="org.apache.cordova.facebook.ConnectPlugin" />
-
You'll need to set up the Facebook SDK for Android:
-
Link the Facebook SDK library to your project. View the properties for the project, and navigate to the 'Android' tab. In the lower part of the dialog, click 'Add' and choose the 'FacebookSDK' project from the workspace.
-
Add a new
com.facebook.LoginActivity
activity to your app to handle Facebook Login. Open up yourAndroidManifest.xml
file and add this additional activity:<activity android:name="com.facebook.LoginActivity" android:label="@string/app_name" />
-
From the Cordova Facebook Plugin folder copy ConnectPlugin.java from
src/android/
folder into the root of your Cordova Android application intosrc/org/apache/cordova/facebook/
. You may have to create that directory path in your project. -
From the Cordova Facebook Plugin folder copy the
www/cdv-plugin-fb-connect.js
,www/facebook_js_sdk.js
andexample/HackBook/
files into your application'sassets/www
folder. Overwrite the existing index.html file. -
Replace your appId in the new index.html file. Leave the quotes.
Now you are ready to create your application! Check out the example
folder for what the HTML, JS etc looks like.
You can run the application from either the command line (ant clean && ant debug install
) or from Eclipse.
NOTE 1: If you are having problems with SBJSON conflicts, download the latest version of git clone the latest cordova-ios code, build the installer, and run the installer to get updated!
NOTE 2: If you're upgrading from SDK 3.0 to 3.1 in the iOS6, you can't ask for both read and write permissions when the user is authenticating. If you do this you'll get a com.facebook.sdk error 2
error alert. This happens due to a flow change in the 3.1 SDK. More info about this can be found in the official docs. Also, make sure you have configured the app's Bundle ID in the Facebook app details, under the "Native iOS App" configuration, otherwise you'll get another com.facebook.sdk error 2
alert. At least, if some of your earlier authentications failed, the device may turn the app to off in Settings > Facebook > Allow These Apps to Use Your Account, so, make sure your app is allowed.
(To be updated) View the Video
Create a basic Cordova iOS application by following the PhoneGap Getting Started Guide
- Download the latest Facebook SDK for iOS from the iOS Dev Center.
- Add the Facebook SDK for iOS Framework by dragging the FacebookSDK.framework folder from the SDK installation folder into the Frameworks section of your Project Navigator.
- Choose 'Create groups for any added folders' and deselect 'Copy items into destination group's folder (if needed)' to keep the reference to the SDK installation folder, rather than creating a copy.
- Add the Facebook SDK for iOS resource bundle by dragging the FacebookSDKResources.bundle file from the FacebookSDK.framework/Resources folder into the Frameworks section of your Project Navigator.
- As you did when copying the Framework, choose 'Create groups for any added folders' and deselect 'Copy items into destination group's folder (if needed)'
- Add the headers by dragging the DeprecatedHeaders folder from the FacebookSDK.framework/Versions/A/DeprecatedHeaders folder into the Frameworks section of your Project Navigator.
- Choose 'Create groups for any added folders' and deselect 'Copy items into destination group's folder (if needed)'. This adds the headers as a reference.
- Click on your project's icon (the root element) in Project Navigator, select your Project, then the Build Settings tab, search for Other Linker Flags.
- Add the value -lsqlite3.0
- Add the value -ObjC
- Click on your project's icon (the root element) in Project Navigator, select your Target, then the Build Phases tab, then the Link Binary With Libraries option.
- Add the Social.framework framework. Make it an optional framework to support pre iOS6 apps.
- Add the Accounts.framework framework. Make it an optional framework to support pre iOS6 apps.
- Add the AdSupport.framework framework. Make it an optional framework to support pre iOS6 apps.
- Add the Security.framework framework. Make it an optional framework to support pre iOS6 apps.
- Locate the plugins section of your Project Navigator and create a group "ios". Make sure it is added as a "group" (yellow folder)
- From the Cordova Facebook Plugin folder copy FacebookConnectPlugin.h and FacebookConnectPlugin.m from the src folder into the new group "ios".
- Find the
config.xml
file in the project navigator and add a new entry as a child to the plugin tag:
<plugin name="org.apache.cordova.facebook.Connect" value="FacebookConnectPlugin" />
- From the Cordova Facebook Plugin folder copy the contents of the www folder into the www directory in Xcode.
- Under the group Resources, find your [PROJECTNAME]-Info.plist, add a new entry. For the key, add FacebookAppID, and its value is your Facebook APP_ID
- Under the group Resources, find your [PROJECTNAME]-Info.plist, right-click on the file and select Open As -> Source Code, add the URL Scheme from the section below (you will need your Facebook APP_ID)
- You can quickly test the examples by following the next instructions then mirror the same process for your app.
- From the example folder, copy either the contents of HackBook folder or the Simple folder into your www directory in Xcode. Overwrite the original index.html file in your project. For HackBook, overwrite the original css and js folders as well.
- Make sure the <script> tags are added and are correct in the index.html. This include a tag for cordova-2.5.0.js, facebook_js_sdk.js and cdv-plugin-fb-connect.js.
- Add your AppID to your index.html. Should be in the callback for the deviceready event. Leave the quotes.
- Run the application in Xcode.
The Facebook SDK will try to access various URLs, and their domains must be whitelisted in your config.xml under access.
You can either add each subdomain separately:
- m.facebook.com
- graph.facebook.com
- api.facebook.com
- *.fbcdn.net
- *.akamaihd.net
Or you can allow all domains with (set to this by default):
- *
Make sure you add the scheme to your [PROJECTNAME]-Info.plist (located as one of the files in your Xcode project), substitute [APP_ID] below to the appropriate values (replace those brackets!). This is to handle the re-direct from Mobile Safari or the Facebook app, after permission authorization.
- [APP_ID] is the Facebook app id given by Facebook
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>fb[**APP_ID**]</string> </array> </dict> </array>
This plugin is based on plugman. To install it to your app, simply execute plugman as follows; It does not currently work with plugman at all. WORK IN PROGRESS
plugman --platform [PLATFORM] --project [TARGET-PATH] --plugin [PLUGIN-PATH] APP_ID="[APP_ID]"
where
[PLATFORM] = ios or android
[TARGET-PATH] = path to folder containing your phonegap project
[PLUGIN-PATH] = path to folder containing this plugin
[APP_ID] = Your APP_ID as registered on Facebook