iOS集成facebook SDK官方文档

来源:互联网 发布:史记哪个版本好 知乎 编辑:程序博客网 时间:2024/04/30 06:32

Getting Started with the Facebook iOS SDK

This guide covers what you need to go through in order to be able to start using the Facebook SDK for iOS, in five easy steps:

  • Step 1: Prerequisites
  • Step 2: Install the Facebook SDK for iOS
  • Step 3: Obtain a Facebook App ID
  • Step 4: Configure your Xcode Project
  • Step 5: Start coding!

Looking for more information about the Facebook Audience Network? Try our Getting Started Guide for Audience Network.

Step 1: Prerequisites

  • OS X is required for all iOS development.
  • You need Xcode. If you don't have it, you can get it from the App Store.
  • Optionally, you may want to use Git.

The Facebook SDK for iOS versions 3.17+ supports iOS 6.x and higher. Support for iOS 5.x was dropped in version 3.17 of the SDK.

Step 2: Install the Facebook SDK for iOS

Download the SDK and install the package by following the instruction wizard. The default install location is ~/Documents/FacebookSDK.

Download iOS SDK


The SDK is also available on GitHub (CocoaPods supported).

Step 3: Obtain a Facebook App ID

If you haven't already registered your app with Facebook by creating a Facebook app, you should create a new app:

Create a New App

 
And fill in the information:

Once your app is created, or if you already had a Facebook app, the app ID is shown at the top of the dashboard page.

If you already have a Facebook app (and corresponding Facebook app ID) that you're usingfor the same app in another platform (for example, Android), you can use the same app ID for iOS.

Next, you need to set the Bundle Identifier and configure your Facebook application to support login from an iOS application.

Select "Settings" from the left-hand nav. Inside the settings, click on "Add Platform" and choose "iOS". Then provide your Bundle Identifier in the "Bundle ID" field and enable "Single Sign On". Don't forget to save the changes!

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

Step 4: Configure your Xcode Project

Add the Facebook SDK for iOS to your project and configure your .plist file.

Add the Facebook SDK

The Facebook SDK for iOS is a framework that you add to your Xcode project. To add the SDK, open~/Documents/FacebookSDK and drag the FacebookSDK.framework 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 reference the SDK where it was installed rather than copying the SDK into your app.

The SDK will automatically load its framework and resource dependencies.

Configure the .plist

Follow these three steps:

  1. Create a key called FacebookAppID with a string value, and add the app ID there.
  2. Create a key called FacebookDisplayName with a string value, and add the Display Name you configured in the App Dashboard.
  3. 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.

The finished .plist should look something like this:

Optionally, if you want all the SDK classes to work "out of the box" you may want to also add the -ObjC linker flag to your project. However, if you choose to do this you will be increasing the size of your executable (due to additional SDK object code loaded into your application). You can learn more about this trade off, what exactly the -ObjC flag does, and how to set it in our troubleshooting guide.

Step 5: Start coding!

The SDK is now installed and set up. You can use the SDK from any of your implementation files by adding the Facebook SDK header file:

#import <FacebookSDK/FacebookSDK.h>

If you want something more complete you can look at one of our feature-based tutorials. These tutorials will walk you through feature implementation and, if relevant, provide sample code and point you to sample apps where you can see the feature in action:

  • Sharing
  • Login with Facebook
  • App Events
  • Making Graph API calls
  • Tell a story with Open Graph
  • Built-in dialogs and UI controls

If you're a games developer we suggest you check out Friend Smash, our game sample. Friend Smash shows to use the Facebook SDK for iOS to make your game more social.

We also have other samples, shipped with the Facebook SDK for iOS or on GitHub. Each demonstrates an important features and techniques for building social apps. You can find a list of all the sample appshere.

0 0