Monday, January 5, 2015

SLComposeViewController - Post a message and an image to Facebook

Update:
December 19, 2015 - The solution below no longer works. It seems that Facebook SDK is required.
January 15, 2016 - See this: Facebook SDK and Swift - Post a message and an image to Facebook

1. Create a UIButton and a UIImageView

2. Include an image file swift.png (or a photo) in the project

3. Write the code as below:


import UIKit
import Social

class ViewController: UIViewController {

    @IBOutlet weak var myImage: UIImageView!
    
    var myUIImage : UIImage!
    
    @IBAction func facebookButtonPressed(sender: UIButton) {
        if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
            var myFBController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            myFBController.setInitialText("Test http://studyswift.blogspot.com")
            myFBController.addImage(self.myUIImage)
            self.presentViewController(myFBController, animated: true, completion: nil)
        } else {
            let alertView = UIAlertView()
            alertView.message = "Please login to Facebook."
            alertView.addButtonWithTitle("OK")
            
            alertView.show()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myUIImage = UIImage(named: "swift.png")
        
        self.myImage.image = myUIImage

Facebook SDK and Swift - Create a Facebook Login Button
Facebook SDK and Swift - Post a message and an image to Facebook
Facebook SDK and Swift - Post a message using Graph API and post an image using FBSDKShareKit

No comments:

Post a Comment