The Facebook SDK supports sharing images and videos from multiple sources: a UIImage already in memory, a local file URL, a PHAsset from the Photos library, or raw Data.
SharePhoto
SharePhoto represents a single image. You can create one from three different sources — only one source is active at a time.
From a UIImage
import FacebookShare
import UIKit
let image: UIImage = // your image
let photo = SharePhoto(image: image, isUserGenerated: true)
From a file URL
The URL must point to a local file on disk. Remote URLs are not supported for this initializer.
let fileURL = URL(fileURLWithPath: "/path/to/photo.jpg")
let photo = SharePhoto(imageURL: fileURL, isUserGenerated: false)
From a PHAsset
import Photos
let asset: PHAsset = // fetched from the Photos library
let photo = SharePhoto(photoAsset: asset, isUserGenerated: true)
The isUserGenerated flag
Set isUserGenerated to true when the photo was taken or chosen by the user, and false when it was generated by your app (for example, a promotional image). This flag is used by the platform to apply appropriate handling.
Caption
You can attach an optional caption to a SharePhoto. Per Facebook Platform Policy (2.3), the caption must come from the user — do not pre-fill it.
photo.caption = userEnteredCaption
SharePhotoContent
SharePhotoContent holds an array of SharePhoto objects to share together.
let photo1 = SharePhoto(image: firstImage, isUserGenerated: true)
let photo2 = SharePhoto(image: secondImage, isUserGenerated: true)
let content = SharePhotoContent()
content.photos = [photo1, photo2]
ShareDialog.show(viewController: self, content: content, delegate: self)
You must provide between 1 and 10 photos. The SDK validates this range and throws if the array is empty or exceeds 10 items.
When sharing via the share sheet, all photos must be UIImage instances — URL-based and asset-based photos are resolved to UIImage internally before the sheet is presented.
ShareVideo
ShareVideo represents a single video. Like SharePhoto, it supports three source types.
From a file URL
let videoURL = URL(fileURLWithPath: "/path/to/video.mov")
let video = ShareVideo(videoURL: videoURL)
From a PHAsset
let asset: PHAsset = // a video PHAsset from the Photos library
let video = ShareVideo(videoAsset: asset)
From raw Data
let videoData: Data = // raw video bytes
let video = ShareVideo(data: videoData)
Adding a preview photo
All three initializers accept an optional previewPhoto parameter. If the preview photo is invalid, the SDK drops it and may generate a default one.
let preview = SharePhoto(image: thumbnailImage, isUserGenerated: false)
let video = ShareVideo(videoURL: videoURL, previewPhoto: preview)
ShareVideoContent
Wrap a ShareVideo in ShareVideoContent to share it.
let video = ShareVideo(videoURL: videoURL)
let content = ShareVideoContent()
content.video = video
content.hashtag = Hashtag("#MyVideo")
ShareDialog.show(viewController: self, content: content, delegate: self)
ShareMediaContent — mixed photos and video
ShareMediaContent lets you combine SharePhoto and ShareVideo objects in a single share. It is currently only available through the share sheet extension.
let photo1 = SharePhoto(image: firstImage, isUserGenerated: true)
let photo2 = SharePhoto(image: secondImage, isUserGenerated: true)
let video = ShareVideo(videoURL: videoURL)
let content = ShareMediaContent()
content.media = [photo1, photo2, video]
let dialog = ShareDialog(viewController: self, content: content, delegate: self)
dialog.mode = .shareSheet
dialog.show()
ShareMediaContent accepts 1–20 media items total, but only one video is allowed per share. If you add more than one ShareVideo to media, validation will throw an error.
ShareMediaContent containing both photos and a video requires the Facebook app to be installed and can only be shared using .shareSheet mode. Attempting to use other modes will fail validation.
Limitations summary
| Content type | Minimum | Maximum | Notes |
|---|
SharePhotoContent photos | 1 | 10 | Photos must be UIImage for the share sheet |
ShareMediaContent media | 1 | 20 | At most 1 video; photos must have UIImage values |
ShareVideoContent videos | 1 | 1 | One video per ShareVideoContent |
Complete example
import FacebookShare
import UIKit
class GalleryShareViewController: UIViewController {
func sharePhotos(_ images: [UIImage]) {
let photos = images.map { SharePhoto(image: $0, isUserGenerated: true) }
let content = SharePhotoContent()
content.photos = photos
ShareDialog.show(viewController: self, content: content, delegate: self)
}
func shareVideo(url: URL) {
let video = ShareVideo(videoURL: url)
let content = ShareVideoContent()
content.video = video
ShareDialog.show(viewController: self, content: content, delegate: self)
}
}
extension GalleryShareViewController: SharingDelegate {
func sharer(_ sharer: Sharing, didCompleteWithResults results: [String: Any]) {
print("Shared")
}
func sharer(_ sharer: Sharing, didFailWithError error: Error) {
print("Error:", error.localizedDescription)
}
func sharerDidCancel(_ sharer: Sharing) {
print("Cancelled")
}
}