Share a Photo

You can share a photo by using the FacebookShareKit framework. This topic shows you how to write the code to share a photo in your app.

To share a photo, you will create the objects for the photo to share and then invoke a share method to share the photo. The two objects for the photo are a SharePhoto object for the photo and a SharePhotoContent object as the model for the photo content.

After you create the objects, you can share the photo in one of the following ways:

This topic shows how to use the Share Dialog.

Due to a limitation with Xcode, you can only test sharing a photo on an iOS device and not in the xCode simulator. Run this this code in a device.

Before You Start

You will need to integrate the FacebookShareKit framework from the Facebook SDK for iOS. Learn more.

Add the following import statement:

import FBSDKShareKit

Step 1: Set the SharePhoto object to the image you want to share

The SharePhoto is the photo that you want to share. Add this code to set the SharePhoto object to the photo.

let photo = SharePhoto(image: image, userGenerated: true)    

Step 2: Set the SharePhotoContent object to the SharePhoto object

The SharePhotoContent is the model of the photo content you want to share. Add this code for the SharePhotoContent object.

let content = SharePhotoContent()
content.photos = [photo]

Step 3: Show the Share Dialog

Add this code to show the Share Dialog.

let dialog = ShareDialog(fromViewController: self, content: content, delegate: self)

// Recommended to validate before trying to display the dialog
do {
    try dialog.validate()
} catch {
    // Handle error
}

dialog.show()