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:
Message Dialog - a dialog to send content through Messenger.
Send Button - sends the photo through Messenger.
Share Dialog - a dialog to share content.
Share Button - sends the photo by showing the Share Dialog.
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 on a device.
You will need to integrate the FacebookShareKit
framework from the Facebook SDK for iOS. Learn more.
Add the following import
statement:
import FBSDKShareKit
SharePhoto
object to the image you want to shareThe 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)
SharePhotoContent
object to the SharePhoto
objectThe 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]
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()