Sunday, January 20, 2013

Getting Started with the Facebook SDK for iOS










The Facebook SDK for iOS is designed to be easy to install and use.
This guide walks you through the basics of setting up the development
 and project environment and creating a simple app.
Watch the screencast or follow the steps below.



1 Install the Prerequisites

You need Xcode 4.5, and optionally, Git
OS X is required for all iOS development. If you're completely new to iOS
development, you'll need Xcode installed on your Mac too. It's available through
the App Store.

2 Install the Facebook SDK for iOS

Download and install the Facebook SDK for iOS
Download the SDK here and install the package. The default install location is
~/Documents/FacebookSDK.
Source code for the SDK is also available on GitHub, and if you have Git installed, you
can create a local repository by cloning it to a location of your choice:
git clone git@github.com:facebook/facebook-ios-sdk.git FacebookSDK
This allows you to keep the SDK up-to-date by simply pulling new commits to the repo.
Note that when building the SDK from source, you must first run scripts/build_framework.sh
before compiling and running the samples.

3 Run the Samples

Try the sample projects in the Samples folder of the installed SDK
The SDK includes a number of samples, each of which demonstrates an
important part of the SDK and various techniques for building social apps.
The following samples are available in the SDK. Simply open the Xcode
 project files inside each of the folders.

Basic samples

  • ProfilePictureSample: demonstrates non-logged-in usage of the profile picture control
  • FriendPickerSample: demonstrates usage of the native friend picker to choose amongst the user's friends
  • PlacePickerSample: demonstrates logged-in usage of the place picker control

Feature samples

  • SessionLoginSample: demonstrates detailed login flow using the SDK
  • JustRequestSample: demonstrates logged-in user making single and batched Facebook API requests
  • BooleanOGSample: demonstrates publishing an Open Graph custom action type
  • SwitchUserSample: demonstrates an approach to allow switching between multiple users

Complete samples

  • HelloFacebookSample: an all-round sample that demonstrates profile access, status updates and photo uploading
  • Scrumptious: demonstrates the use of login, requests, pickers, picture uploads, and Open Graph publishing
  • Hackbook: another sample app demonstrating a selection of Graph API calls
Note that to see the native Login Dialog and the native share dialogs in action, you
must first log in to your Facebook account on your iOS device: Settings > Facebook.

4 Create a Facebook App

You will need a Facebook App ID before you create a new project with the SDK
Create a new app on the Facebook App Dashboard, enter your app's basic information.

Once created, note the app ID shown at the top of the dashboard page.

Alternatively, you can of course use the ID of an existing app.
Now, you need to set the Bundle Identifier and configure your Facebook application to support login from an iOS application.
(Note: this is a new step in the Facebook SDK for iOS v3.1. Previous versions of the SDK did not require this step.)
Click on the checkmark next to “Native iOS App” and supply your Bundle Identifier in the “Bundle ID” field. In addition, make sure the Facebook Login radio button is set to “Enabled”.

You can find or set your Bundle Identifier in Xcode in the Project tab.

5 Configure a new Xcode Project

Create a new Xcode project, add the Facebook SDK for iOS, its dependencies, and the Facebook App ID to its settings
In Xcode, create a new project, and select 'Single View Application' as shown below.

Provide a name and identifier as required:

Adding SDK & framework dependencies

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)'
You can also add the FBUserSettingsViewResources bundle in the same way if you expect to use that controller in your app.
The SDK relies on three other frameworks (AdSupport, Accounts and Social) to use the Facebook features built into iOS6. To add these, go to the 'Linked Frameworks and Libraries' section of the target's Summary pane, and click the 'plus' button to add them:

Note: You should use the 'Optional' flag for iOS6-specific frameworks if you would like your app to also build for older versions of the operating systems.

Once done, your Project Navigator should look something like this:

Adding SQLite

Next you need to add the ''-lsqlite3.0'' SQL library to the list of build dependencies in the Build Settings pane:

Adding your Facebook App ID

Finally, you need to place the Facebook app ID in two places in your application's main .plist file. Create a key called FacebookAppID with a string value, and add the app ID there:

Also, create an array key called URL types with a single array sub-item called URL Schemes. Give this a single item with your app ID prefixed with fb:

(This is used to ensure the application will receive the callback URL of the web-based OAuth flow.)

6 Start Coding!

Get started by enhancing one of the sample apps, or working through the Scrumptious Tutorial.

Source: Facebook.

No comments:

Post a Comment