With this Swift code example I would like to share with you how to implement Facebook Share Photo button which will share a photo user selects to user’s Facebook timeline. The two code examples below will cover:
- Set up UIButton to trigger the UIImagePickerController to let user select photo they would like to share.
- Create UIImageView to preview the selected image
- Use FBSDKSharePhoto to create a sharable photo object
- Use FBSDKSharePhotoContent to set an array of photo objects. User can share multiple photos to their Facebook time line if FBSDKSharePhotoContent.photos is given more than one photo object.
- Add Facebook share button FBSDKShareButton as subview to main view
Here is how the below code example will look like if you run the complete code example provided below.
Here is a short Swift code example which demonstrates how to pick image from iOS UIImagePickerController and use it with Facebook SDK for iOS to share selected photo to Facebook timeline.
Facebook iOS SDK. Share photo Swift Code Example.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { myImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage myImageView.backgroundColor = UIColor.clearColor() myImageView.contentMode = UIViewContentMode.ScaleAspectFit self.dismissViewControllerAnimated(true, completion: nil) let photo:FBSDKSharePhoto = FBSDKSharePhoto() photo.image = myImageView.image photo.userGenerated = true let content:FBSDKSharePhotoContent = FBSDKSharePhotoContent() content.photos = [photo] let shareButton = FBSDKShareButton() shareButton.center = view.center shareButton.shareContent = content shareButton.center = self.view.center self.view.addSubview(shareButton) }
If you are looking for a complete Swift code example you can copy and paste, here is a complete ViewController with Facebook iOS Framework import statements. All you need to do is to copy and paste it into your project.
Facebook SDK for iOS. Share Photo Button Complete Swift Code Example.
import UIKit import FBSDKCoreKit import FBSDKShareKit class SharePhotoViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{ var myImageView: UIImageView! var showImagePicketButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. setupImagePickerButton() setupImageView() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func setupImagePickerButton() { let button = UIButton(type: UIButtonType.System) as UIButton let screenSize: CGRect = UIScreen.mainScreen().bounds let screenWidth = screenSize.width let buttonWidth:CGFloat = 150 let buttonHeight:CGFloat = 45 let xPostion:CGFloat = (screenWidth/2)-(buttonWidth/2) let yPostion:CGFloat = 70 button.frame = CGRectMake(xPostion, yPostion, buttonWidth, buttonHeight) button.backgroundColor = UIColor.lightGrayColor() button.setTitle("Select photo", forState: UIControlState.Normal) button.tintColor = UIColor.blackColor() button.addTarget(self, action: #selector(SharePhotoViewController.displayImagePickerButtonTapped) , forControlEvents: .TouchUpInside) self.view.addSubview(button) } func setupImageView() { myImageView = UIImageView() let screenSize: CGRect = UIScreen.mainScreen().bounds let screenWidth = screenSize.width let imageWidth:CGFloat = 200 let imageHeight:CGFloat = 200 let xPostion:CGFloat = (screenWidth/2) - (imageWidth/2) let yPostion:CGFloat = 100 myImageView.frame = CGRectMake(xPostion, yPostion, imageWidth, imageHeight) self.view.addSubview(myImageView) } func displayImagePickerButtonTapped() { let myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary self.presentViewController(myPickerController, animated: true, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { myImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage myImageView.backgroundColor = UIColor.clearColor() myImageView.contentMode = UIViewContentMode.ScaleAspectFit self.dismissViewControllerAnimated(true, completion: nil) let photo:FBSDKSharePhoto = FBSDKSharePhoto() photo.image = myImageView.image photo.userGenerated = true let content:FBSDKSharePhotoContent = FBSDKSharePhotoContent() content.photos = [photo] let shareButton = FBSDKShareButton() shareButton.center = view.center shareButton.shareContent = content shareButton.center = self.view.center self.view.addSubview(shareButton) } }
Checkout more Swift code examples at Swift Code Examples page.
[raw_html_snippet id=”cookbookpagecoursesheader”]
Unit Testing Swift Mobile App
Apply Test-Driven Development(TDD) process to iOS mobile app development in Swift Preview this video course.