| Platform | Since |
|---|---|
| Android | 0.8 |
| iPhone | 0.8 |
| iPad | 0.8 |
The top level Facebook module.
The Facebook module is used for connecting your application with
Facebook through the Facebook Graph API
(see requestWithGraphPath) or the deprecated
Facebook REST API (see request).
Due to how the facebook login process works on iOS, your app will need to have the following
in your tiapp.xml if you target those platforms:
<property name="ti.facebook.appid">your_facebook_app_id_here</property>
You must still set Ti.Facebook.appid within your app itself to use the facebook module.
This property is used only for configuring your app to interface with the facebook
login process.
Titanium Mobile SDK 1.6.0 marked a significant change for this module. Older versions of
the module were not compatible with the new Graph API. Applications written for the
pre-1.6.0 version of this module will not work with the 1.6.0 and higher versions.
Nor is the code shown in the examples here backwards-compatible with pre-1.6.0 Titanium
Mobile SDK.
Shows official Facebook dialog for logging in the user and prompting the user to approve your requested permissions. Listen for the module's "login" event to determine success/failure.
Titanium.Facebook.appid = '[YOUR APPID]'; Titanium.Facebook.permissions = ['publish_stream']; // Permissions your app needs Titanium.Facebook.addEventListener('login', function(e) { if (e.success) { alert('Logged In'); } else if (e.error) { alert(e.error); } else if (e.cancelled) { alert("Cancelled"); } }); Titanium.Facebook.authorize();
Logout the user and forget the authorization token. Listen for the module's "logout" event to determine when logout is finished.
Titanium.Facebook.addEventListener('logout', function(e) { alert('Logged out'); }); Titanium.Facebook.logout();
We've provided the Facebook-themed LoginButton which updates its state automatically depending on whether the user is logged-in or not. I.e., when the user is logged-in, then the button will show "Logout", and vice-versa.
Note that you don't need to set a click listener or anything else on the button. It "just works". To listen for the actual login and logout events (which are part of the Titanium Facebook module and not specific to the login button), add listeners at the module level as in the example below.
// Don't forget to set your appid and requested permissions, else the login button // won't be effective. Titanium.Facebook.appid = '[your appid]'; Titanium.Facebook.permissions = ['publish_stream']; Titanium.Facebook.addEventListener('login', function(e) { if (e.success) { alert('Logged in'); } }); Titanium.Facebook.addEventListener('logout', function(e) { alert('Logged out'); }); // add the button. Note that it doesn't need a click event or anything. Titanium.UI.currentWindow.add(Titanium.Facebook.createLoginButton({ top: 50, style: 'wide' }));
The style:'wide' shows a wide version of the button that displays "Connect with Facebook" instead of just "Connect".
This example makes a call to the "me" graph path and displays the results, which will be JSON from Facebook. It assumes the user is already logged-in (you can check this with Titanium.Facebook.loggedIn.)
Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', function(e) { if (e.success) { alert(e.result); } else if (e.error) { alert(e.error); } else { alert('Unknown response'); } });
This example uses the Graph API to create an event in the logged-on user's Facebook account. This requires the "create_event" permission.
// First make sure this permission exists Titanium.Facebook.permissions = ['create_event']; Titanium.Facebook.authorize(); // ... // ... // Now create the event after you've confirmed authorize() was successful. var starttime = new Date(2011, 4, 31, 17, 0); var endtime = new Date(2011, 4, 31, 19, 0); var title = "Barry's Birthday Celebration"; var description = "Barry will have a great party"; var data = { start_time: JSON.stringify(starttime), // API expects a JSON stringified date end_time: JSON.stringify(endtime), summary: description, name: title }; Titanium.Facebook.requestWithGraphPath('me/events', data, 'POST', function(e) { if (e.success) { alert("Success! Returned from FB: " + e.result); } else { if (e.error) { alert(e.error); } else { alert("Unknown result"); } } });
Use the Graph API to set the user's Facebook status. Requires the "publish_stream" permissions.
// First make sure this permission exists Titanium.Facebook.permissions = ['publish_stream']; Titanium.Facebook.authorize(); // ... // ... // Now create the status message after you've confirmed that authorize() succeeded Titanium.Facebook.requestWithGraphPath('me/feed', {message: "Trying out FB Graph API and it's fun!"}, "POST", function(e) { if (e.success) { alert("Success! From FB: " + e.result); } else { if (e.error) { alert(e.error); } else { alert("Unkown result"); } } });
This example posts a photo to the user's account using the Graph API. Another example below shows how to do this with the REST API, if desired. This requires the "publish_stream" permission.
// First make sure this permission exists Titanium.Facebook.permissions = ['publish_stream']; Titanium.Facebook.authorize(); // ... // ... // Now post the photo after you've confirmed that authorize() succeeded var f = Ti.Filesystem.getFile('pumpkin.jpg'); var blob = f.read(); var data = { message: 'This is a pumpkin', picture: blob }; Titanium.Facebook.requestWithGraphPath('me/photos', data, 'POST', function(e){ if (e.success) { alert("Success! From FB: " + e.result); } else { if (e.error) { alert(e.error); } else { alert("Unkown result"); } } });
This example posts a photo to the user's account using the REST API. Another example above shows how to do this with the Graph API. This requires the "publish_stream" permission.
// First make sure this permission exists Titanium.Facebook.permissions = ['publish_stream']; Titanium.Facebook.authorize(); // ... // ... // Now post the photo after you've confirmed that authorize() succeeded var f = Ti.Filesystem.getFile('pumpkin.jpg'); var blob = f.read(); var data = { caption: 'This is a pumpkin', picture: blob }; Titanium.Facebook.request('photos.upload', data, function(e){ if (e.success) { alert("Success! From FB: " + e.result); } else { if (e.error) { alert(e.error); } else { alert("Unkown result"); } } });
This example shows how to display the official Facebook dialog for making a post to the user's feed. In this example, we'll prefill some of the feed fields by passing a data dictionary to the dialog() method; this is not required.
var data = { link: "http://www.appcelerator.com", name: "Appcelerator Titanium Mobile", message: "Checkout this cool open source project for creating mobile apps", caption: "Appcelerator Titanium Mobile", picture: "http://developer.appcelerator.com/assets/img/DEV_titmobile_image.png", description: "You've got the ideas, now you've got the power. Titanium translates your hard won web skills into native applications..." }; Titanium.Facebook.dialog("feed", data, function(e) { if (e.success) { alert("Success! From FB: " + e.result); } else { if (e.error) { alert(e.error); } else if (e.cancelled) { alert('Cancelled'); } else { alert("Unkown result"); } } });
| Name | Summary |
|---|---|
| Titanium.Facebook.LoginButton |
A Facebook login button. |
| Name | Summary |
|---|---|
| addEventListener |
Adds the specified callback as an event listener for the named event. |
| authorize |
Login the user (if not already logged in) and authorize your application. Be sure to set your desired |
| createLoginButton |
Create and return an instance of |
| dialog |
Open a supported Facebook dialog. "feed" is just about the only useful one. |
| fireEvent |
Fires a synthesized event to any registered listeners. |
| getAccessToken |
Gets the value of the accessToken property. |
| getAppid |
Gets the value of the appid property. |
| getExpirationDate |
Gets the value of the expirationDate property. |
| getForceDialogAuth |
Gets the value of the forceDialogAuth property. |
| getLoggedIn |
Gets the value of the loggedIn property. |
| getPermissions |
Gets the value of the permissions property. |
| getUid |
Gets the value of the uid property. |
| logout |
Clear the OAuth |
| removeEventListener |
Removes the specified callback as an event listener for the named event. |
| request |
Make a request to the deprecated Facebook REST API. |
| requestWithGraphPath |
Make a Facebook Graph API request. If the request requires user authorization, be sure user is already logged-in and your app is authorized. (You can check |
| setAccessToken |
Sets the value of the accessToken property. |
| setAppid |
Sets the value of the appid property. |
| setExpirationDate |
Sets the value of the expirationDate property. |
| setForceDialogAuth |
Sets the value of the forceDialogAuth property. |
| setLoggedIn |
Sets the value of the loggedIn property. |
| setPermissions |
Sets the value of the permissions property. |
| setUid |
Sets the value of the uid property. |
| Name | Type | Summary |
|---|---|---|
| accessToken | String |
OAuth token set after a successful |
| appid | String |
your Facebook application id. You need to set this for anything to work. |
| expirationDate | Date |
Time at which the |
| forceDialogAuth | Boolean |
Set to false to enable "Single-Sign-On" in cases where the official Facebook app is on the device. Default is true, meaning the traditional, dialog-based authentication is used rather than Single-Sign-On. See Facebook Mobile Guide for details of their Single-Sign-On schem. |
| loggedIn | Boolean |
returns true if the user has logged in |
| permissions | Object |
set/return an array of permissions to request for your app. Be sure the permissions you want are set before calling |
| uid | String |
the unique user id returned from Facebook. |
| Name | Summary |
|---|---|
| login |
fired at session login |
| logout |
fired at session logout |