Sharing Content Types

Currently, the Facebook SDK for Swift can share 4 different kinds of content:

  • Links - Represented by the LinkShareContent object.
  • Photos - Represented by the PhotoShareContent object.
  • Videos - Represented by the VideoShareContent object.
  • Open Graph - Represented by the OpenGraphShareContent object.

Links

The details of shared links are specified with the LinkShareContent object. When people share links from your app to Facebook, it includes properties that show up in the post:

Property Description

url

Specifies the link to be shared.

title

Specifies the title of the content in the link.

imageURL

Specifies the URL of the thumbnail image to display on the post.

description

Specifies the description of the content. This is usually 2-4 sentences.

The following code example demonstrates how to initiate a link share.

import FacebookShare

let content = LinkShareContent(url: NSURL("https://developers.facebook.com"))
try ShareDialog.show(from: myViewController, content: content)  

When your app shares links to the iTunes or Google Play stores, the images and description you provide will be ignored. Instead, information scraped from the store is used. This keeps the content in sync with what the App developer has listed. In some cases, the scraped content may not include images. To preview a link share to iTunes or Google Play, enter your URL into the Sharing Debugger.

Photos

The details of shared photos are specified with the PhotoShareContent object. In order to share photos, the user must have the native Facebook for iOS app installed. Photos must be less than 12MB in size.

The following code example demonstrates how to initiate a photo share.

import FacebookShare

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSString:AnyObject]) {
  guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
    return // No image selected.
  }

  let photo = Photo(image: image, userGenerated: true)
  let content = PhotoShareContent(photos: [photo])
  try ShareDialog.show(from: myViewController, content: content)
}

Videos

The details of shared videos are specified with the VideoShareContent object. In order to share videos, the user must have the native Facebook for iOS app installed. Videos must be less than 12MB in size.

The following code example demonstrates how to initiate a video share.

import FacebookShare

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSString:AnyObject]) {
  guard let videoURL = info[UIImagePickerControllerReferenceURL] as? NSURL else {
    return // No video selected.
  }

  let video = Video(url: videoURL)
  let content = VideoShareContent(video: video)
  try ShareDialog.show(from: myViewController, content: content)
}

Use the previewPhoto property of the VideoShareContent object to specify a preview image for your video.