
There are a couple of ways to integrate Facebook Sharing into your app. The first and easiest is using the existing Social Framework that is embedded in iOS. If you want to go this route, read my article about it here: Tutorial: Share on Twitter and Facebook in Swift.
The other option is to use the native Facebook SDK. This will require that a user is logged into Facebook using Facebook Login. In this tutorial we’ll cover how to integrate Facebook Share into your app using the native Facebook 4.0 SDK using Swift.
You can share three different types of content using Facebook, they are Links, Photos & Videos. Let’s cover each of them.
To begin, follow the steps in this article to integrate the Facebook SDK and implement Facebook Login: Tutorial: How To Use Login in Facebook SDK 4.0 for Swift.
Import the Share Kit Framework
- Add the FacebookSDKShareKit.Framework & Bolts.framework to your project just like your did with the FacebookSDKCoreKit.Framework. Drag it or add it using the “Linked Frameworks and Libraries” within your target settings.
- Add the following import statement to your Bridging-Header.h, right below the Core Kit entry.
#import
- Add the Facebook Share buttons to your ViewController.swift.
Share a Link on Facebook
When people share links from your app to Facebook, it includes attributes that show up in the post:
contentURL – the link to be shared
contentTitle – represents the title of the content in the link
imageURL – the URL of thumbnail image that appears on the post
contentDescription – of the content, usually 2-4 sentences
Here’s the code to show and execute the Link button:
let content : FBSDKShareLinkContent = FBSDKShareLinkContent() content.contentURL = NSURL(string: "") content.contentTitle = " " content.contentDescription = " " content.imageURL = NSURL(string: " ") let button : FBSDKShareButton = FBSDKShareButton() button.shareContent = content button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25) self.view.addSubview(button)
Share a Photo on Facebook
People can share photos from your app to Facebook with the Share Dialog or with a custom interface:
- Photos must be less than 12MB in size
- People need the native Facebook for iOS app installed, version 7.0 or higher
Build your share content for photos with the FBSDKSharePhotoContent model.
Note: You will not be able to test this in the Simulator, you’ll need to test on your device.
Here’s the code to show and execute the Photo button from an image picker:
First you’ll need to add the UIImagePickerControllerDelgate and UINavigationController Delegate to your class definition.
class ViewController: UIViewController, FBSDKLoginButtonDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
Then add the code to display a button on the screen for the User to select a photo:
let button : UIButton = UIButton() button.backgroundColor = UIColor.blueColor() button.setTitle("Choose Photo", forState: .Normal) button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 150) * 0.5, 125, 150, 25) button.addTarget(self, action: "photoBtnClicked", forControlEvents: .TouchUpInside) self.view.addSubview(button) let label : UILabel = UILabel() label.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 200) * 0.5, 100, 200, 25) label.text = "Photos Example" label.textAlignment = .Center self.view.addSubview(label)
When the User clicks the button, the following method is called to show the iOS image picker:
func photoBtnClicked(){ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){ println("Photo capture") imagePicker.delegate = self imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum; imagePicker.allowsEditing = false self.presentViewController(imagePicker, animated: true, completion: nil) } }
Once the User selects a photo, the following delegate method is called.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { let photo : FBSDKSharePhoto = FBSDKSharePhoto() photo.image = info[UIImagePickerControllerOriginalImage] as! UIImage photo.userGenerated = true let content : FBSDKSharePhotoContent = FBSDKSharePhotoContent() content.photos = [photo] }
Share a Video on Facebook
People using your app can share videos to Facebook with the Share dialog or with your own custom interface:
- Videos must be less than 12MB in size
- People who share should have Facebook for iOS client installed, version 26.0 or higher.
Build your share content for photos with the FBSDKShareVideoContent model.
Note: You will not be able to test this in the Simulator, you’ll need to test on your device.
Here’s the code to show and execute the Video button from an image picker:
First you’ll need to add the UIImagePickerControllerDelgate and UINavigationController Delegate to your class definition.
class ViewController: UIViewController, FBSDKLoginButtonDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
Then add the code to display a button on the screen for the User to select a video:
let button : UIButton = UIButton() button.backgroundColor = UIColor.blueColor() button.setTitle("Choose Video", forState: .Normal) button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 150) * 0.5, 200, 150, 25) button.addTarget(self, action: "videoBtnClicked", forControlEvents: .TouchUpInside) self.view.addSubview(button) let label : UILabel = UILabel() label.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 200) * 0.5, 175, 200, 25) label.text = "Video Example" label.textAlignment = .Center self.view.addSubview(label)
When the User clicks the button, the following method is called to show the iOS video picker:
func videoBtnClicked(){ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){ println("Video capture") imagePicker.delegate = self imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum imagePicker.allowsEditing = false self.presentViewController(imagePicker, animated: true, completion: nil) } }
Once the User selects a video, the following delegate method is called.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { let video : FBSDKShareVideo = FBSDKShareVideo() video.videoURL = info[UIImagePickerControllerMediaURL] as! NSURL let content : FBSDKShareVideoContent = FBSDKShareVideoContent() content.video = video }
You can grab the full source code for this tutorial. Note: Created using XCode 6.3 (Swift 1.2).
If you need it built using Xcode 6.2 (Swift 1.1).