1

I would like to make Facebook share button on my app that shares the image that is loaded from the UIImageview. I cannot figure out how to do it.. There is option to add the text on Facebook share window, but can't add the image. Please help.

import UIKit
import Social

class ViewController: UIViewController {


    @IBOutlet weak var saveButtonVar: UIButton!
    @IBOutlet weak var image: UIImageView!

    @IBAction func facebookBtn(sender: AnyObject) {
        var facebookBtn : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            facebookBtn.setInitialText("I am sharing my motivation with you!")
            facebookBtn.addImage(UIImage(named: "motImg")) //????
        self.presentViewController(facebookBtn, animated: true, completion: nil)

    }

    @IBAction func twitterBtn(sender: AnyObject) {

    }





    @IBAction func saveButton(sender: AnyObject) {

        UIImageWriteToSavedPhotosAlbum(image.image, nil, nil, nil)

        let alertController = UIAlertController(title: "Image Saved!", message:
            "", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)


    }




    @IBAction func button(sender: AnyObject) {

        load_image("https://dl.dropboxusercontent.com/u/26347781/Images/Image\(arc4random_uniform(17) + 1).jpg")

        saveButtonVar.hidden = false


    }

    func load_image(urlString:String)
    {

        var imgURL: NSURL = NSURL(string: urlString)!
        let request: NSURLRequest = NSURLRequest(URL: imgURL)
        NSURLConnection.sendAsynchronousRequest(
            request, queue: NSOperationQueue.mainQueue(),
            completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                if error == nil {
                    self.image.image = UIImage(data: data)
                }
        })

    }


    override func viewDidLoad() {

        super.viewDidLoad()

        saveButtonVar.hidden = true

        image.image = UIImage(named: "motImg")



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}
Linas Vildziunas
  • 171
  • 1
  • 6
  • 12

2 Answers2

6

Swift 4

Integrate FacebookShare sdk.

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

@IBAction func facebookBtn(sender: AnyObject) {
  let shareImage = SharePhoto()
  shareImage.image = imageView.image //Image from your imageview
  shareImage.isUserGenerated = true

  let content = SharePhotoContent()
  content.photos = [shareImage]

  let sharedDialoge = ShareDialog()
  sharedDialoge.shareContent = content

  sharedDialoge.fromViewController = self
  sharedDialoge.mode = .automatic


  if(sharedDialoge.canShow)
  {
    sharedDialoge.show()
  }
  else
  {
    print("Install Facebook client app to share image")
  }
}
Jithin
  • 101
  • 1
  • 7
0

Hei there!

Check out this guy's answer: Swift UIActivityViewController Image&Url Share not working with FB

For many people it seemed to work the example in the link, so maybe it does for you, too. However, so far it did not work for me and I came across your question while still investigating my problem.

As far as adding text, I know this functionality is off for some time, but you should be able to add an url link.

Let me know how this works for you.

Community
  • 1
  • 1
asheyla
  • 3,175
  • 5
  • 18
  • 34