Share Button for iOS

With the Share Button you will allow people to share content to their Facebook timeline, to a friend's timeline or in a group.

Sharing on Mobile - Step by Step

The Share Button on mobile follows the same userflow like you know it from the Share Button for web: After tapping or clicking the button, the user will have options to add a message, customize the target audience and eventually confirm the share.

  1. Share Button: Will be displayed without share counter. User taps the Share Button, a new window will open (also known as fast-app-switch).
  2. Share Dialog: The window will show a post composer containing a previw of the shared link. If the user installed the Facebook app for iOS the share dialog will be served by the Facebook app otherwise by the Safari Browser. Via tap on the Share button the user will confirm the share and in the following come back to your application.

Adding the Share Button to your Application

1. General iOS Setup

2. Add Frameworks

3. Add Share button code

1. General iOS Setup

Complete the Getting Started Guide for Facebook iOS SDK before adding the Share button. The guide will include:

  • Setting up a Facebook application.
  • Downloading and installing the Facebook iOS SDK.
  • Adding the Facebook iOS SDK to your project.
Facebook SDK for iOS - Getting Started

2. Add Frameworks

To use the Share button in your view you need to add the Frameworks FBSDKCoreKit and FBSDKShareKit. You can find both in the Facebook iOS directory (~/Documents/FacebookSDK).

  1. Drag & drop the frameworks to your Frameworks folder in Xcode's project explorer.
  2. Add the following #import calls to the header of your view controller:
// For example in ViewController.m after `#import "ViewController.h"`
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>

3. Add Share Button Code

You can add the Share button to your view like you would any other UIView. Create a new Share button FBSDKShareButton instance as shown in the code example shown below. Next assign an object of the type FBSDKShareLinkContent to the property shareContent. You must at the very least either share a link URL (initWithContentURL) or an image URL (imageURL). All other values are optional.

//  ViewController.m
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
  content.contentURL = [NSURL
    URLWithString:@"https://www.facebook.com/FacebookDevelopers"];
  FBSDKShareButton *shareButton = [[FBSDKShareButton alloc] init];
  shareButton.shareContent = content;  
  shareButton.center = self.view.center;
  [self.view addSubview:shareButton];
}
@end

Customizing Shared Content

Generally there are two options to customize your shared content:

  1. Using Open Graph Tags.
  2. Adjust FBSDKShareLinkContent selectors

FBSDKShareLinkContent Selectors

Below we show which properties you can use and which values they must have. Please keep in mind that the post composer result will depend on the user's device configuration as well. E.g. the property placeID is currently only available for users having the Facebook app for iOS installed on their device.

Selector Description Type

initWithContentURL

URL for the content being shared.

NSURL

contentTitle

The link title

NSString

contentDescription

The description of the link

NSString

imageURL

The network URL of an image

NSURL

peopleIDs

Array of IDs for people to tag (NSString)

NSArray

placeID

The ID for a place to tag with this content.

NSString

ref

The ref for the content

NSString

Usage example for FBSDKShareLinkContent Selectors

You can use the example posted below for testing the Share button in your application. User IDs used for peopleIDs must be friends of the current user. You can use the Graph API Explorer to quickly obtain friends IDs. If you do not know about friends permissions please refer to the Facebook Login permissions guide.

NSURL *contentURL = [[NSURL alloc] initWithString:
  @"http://en.wikipedia.org/wiki/Facebook"];

NSURL *imageURL = 
  [NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/commons/thumb/9/95/Facebook_Headquarters_Menlo_Park.jpg/2880px-Facebook_Headquarters_Menlo_Park.jpg"];
  
button.shareContent = [[FBSDKShareLinkContent alloc]
  initWithContentURL: contentURL
  contentTitle: @"My Share Title"
  contentDescription: @"Lorem ipsum dolor sit amet."
  imageURL: imageURL
  peopleIDs: @[@"1561082740838259"]
  placeID: @"166793820034304"
  ref: @"myRefId"
];

Display Example

For a person the result of the example code shown above will look somewhat like this: