How to Integrate the Facebook SDK for Social Sharing in iOS

Integrate the Facebook SDK for social sharing in iOS using FacebookShare framework with ShareDialog and SharePhotoContent. Apple removed Social framework support for Facebook in iOS 11. You now need Facebook's official iOS SDK, available through Swift Package Manager at github.com/facebook/facebook-ios-sdk. The SDK handles authentication, app switching, and posting to Facebook with a few lines of Swift code.

How to Install the Facebook SDK with Swift Package Manager

Open your Xcode 26 project. Select the project in the navigator. Right-click and choose Add Package Dependencies. Enter the package URL: https://github.com/facebook/facebook-ios-sdk. Click Add Package. Select FacebookCore, FacebookLogin, and FacebookShare for your app target. Swift Package Manager handles version resolution automatically. The SDK compiles as part of your project, and Xcode resolves dependencies before each build.

How to Create and Configure a Facebook App

Every iOS app that uses Facebook APIs needs a registered Facebook app. Go to developers.facebook.com and log in. Click My Apps and then Create App. Select Other and then Consumer as the app type. Set the display name to your app name such as SocialAppDemo. Complete the registration. After creation, navigate to Settings and then Basic to find your App ID.

How to Configure Info.plist for the Facebook SDK

Facebook SDK reads configuration from Info.plist. Open Info.plist as source code by right-clicking and selecting Open As and then Source Code. Add these entries before the closing dict tag:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb137896523578923</string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>137896523578923</string>
<key>FacebookDisplayName</key>
<string>Social Sharing Demo</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

Replace 137896523578923 with your actual Facebook App ID. The URL scheme format is fb followed by your App ID. LSApplicationQueriesSchemes entries enable communication with the native Facebook app for authentication and sharing.

How to Implement Photo Sharing with ShareDialog

Import the FacebookShare framework in your view controller:

import FacebookShare

Create a SharePhotoContent object and present ShareDialog:

let facebookAction = UIAlertAction(title: "Facebook", style: .default) { action in
    let selectedImageName = self.restaurantImages[indexPath.row]
    guard let selectedImage = UIImage(named: selectedImageName) else { return }
    
    let photo = SharePhoto(image: selectedImage, isUserGenerated: false)
    let content = SharePhotoContent()
    content.photos = [photo]
    
    let shareDialog = ShareDialog(viewController: self, content: content, delegate: nil)
    shareDialog.show()
}

SharePhoto wraps a UIImage for Facebook's sharing system. SharePhotoContent holds the photos array. ShareDialog intelligently selects the best sharing method. It uses the native Facebook app if installed. It falls back to a web-based dialog if Facebook is not installed.

How to Share Links with ShareLinkContent

ShareLinkContent creates preview cards with page title, description, and image:

let content = ShareLinkContent()
content.contentURL = URL(string: "https://www.appcoda.com")!
let shareDialog = ShareDialog(viewController: self, content: content, delegate: nil)
shareDialog.show()

Links are ideal for sharing articles, product pages, or any URL content.

How to Share Videos with ShareVideoContent

Video sharing follows the same pattern:

let video = ShareVideo(videoURL: videoFileURL)
let content = ShareVideoContent()
content.video = video
let shareDialog = ShareDialog(viewController: self, content: content, delegate: nil)
shareDialog.show()

Wrap your video file URL in ShareVideo. Add it to ShareVideoContent. Present with ShareDialog.

Testing Facebook Sharing on a Real Device

Facebook sharing requires the native Facebook app. The iOS simulator does not have it. Test on a physical iPhone or iPad with Facebook installed. Build and run the app. Tap the share button. The app switches to Facebook with a pre-populated post. After posting, Facebook returns control to your app. If Facebook is not installed, the SDK uses Safari web fallback automatically.

Troubleshooting Common Facebook SDK Issues

The This app is not configured correctly error indicates a mismatched App ID. Verify the App ID in Info.plist matches the one in your Facebook developer dashboard. Ensure the bundle ID in Xcode matches the one registered in your Facebook app settings. If the share dialog does not appear, verify CFBundleURLSchemes is formatted correctly as fb followed by your numeric App ID. If the app crashes on share, confirm FacebookCore and FacebookShare are both added under Frameworks, Libraries, and Embedded Content in your target settings. If the dialog dismisses immediately, check the Xcode console for SDK error messages.

Best Practices for Facebook Social Sharing

Pre-populate content thoughtfully but let users customize the message. Never auto-post without user confirmation. Implement SharingDelegate to know whether sharing succeeded or failed and show appropriate feedback. Respect user privacy by sharing only content the user explicitly selected. Offer multiple sharing platforms beyond Facebook. Test both paths with and without the native Facebook app installed to verify the web fallback works correctly.

Summary

This tutorial covered the complete implementation process from setup to working code. You now have a functional implementation that you can extend and customize for your own projects. The same patterns apply to related features in the iOS ecosystem.

Frequently Asked Questions

What Xcode version do I need for this tutorial? This tutorial uses Xcode 26 with Swift 6.2. The code examples work with any recent version of Xcode that supports the frameworks used here.

Can I test this on the iOS Simulator? Some features require a physical device with a compatible processor. The article notes specific sections where the Simulator is sufficient and where a real device is required.

What iOS version is required? This tutorial targets iOS 11. Most of the APIs shown work on earlier versions as well, but some newer APIs may require the version specified in the article.

What are the prerequisites for this tutorial? You need Xcode installed on a Mac and basic familiarity with Swift programming. No prior experience with the specific framework is required.

All articles