With the Share Button you will allow people to share content to their Facebook timeline, to a friend's timeline or in a group.
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.
Complete the Getting Started Guide for Facebook iOS SDK before adding the Share button. The guide will include:
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
).
Frameworks
folder in Xcode's project explorer. #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>
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 share a link URL (initWithContentURL
), but 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
Generally there are two options to customize your shared content:
FBSDKShareLinkContent
selectors FBSDKShareLinkContent
SelectorsBelow 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 |
---|---|---|
| URL for the content being shared. |
|
| Array of IDs for people to tag ( |
|
| The ID for a place to tag with this content. |
|
| The |
|
FBSDKShareLinkContent
SelectorsAs of April 18, 2017, the following parameters are no longer supported by Graph API versions 2.9 and higher. For versions 2.8 and lower, the parameters will continue working until July 17, 2017.
Selector | Description | Type |
---|---|---|
| The link title |
|
| The description of the link |
|
| The network URL of an image |
|
FBSDKShareLinkContent
SelectorsYou 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"]; button.shareContent = [[FBSDKShareLinkContent alloc] initWithContentURL: contentURL peopleIDs: @[@"1561082740838259"] placeID: @"166793820034304" ref: @"myRefId" ];
For a person the result of the example code shown above will look somewhat like this: