FBSDKGraphRequest. Publish Message to User’s Feed on Facebook.

With this Swift code example I would like to share with you how to use FBSDKGraphRequest from Facebook’s SDK for iOS to publish a text message to user’s Feed on Facebook. The code example below will cover:

  • Use FBSDKAccessToken to check if user has granted “publish_actions” permissions. If not, then request “publish_actions”,
  • Read text message from UITextView when the Send button is tapped,
  • Use FBSDKGraphRequest to publish a text message to user’s Feed on Facebook when Send button is tapped,
  • UITextView and the Send button are added to the View using Xcode interface designer rather than programmatically. But if you do it programmatically, Facebook FBSDKGraphRequest will still work.

FBSDKGraphRequest – Publish Message to User’s Feed. Complete Swift Code Example.

  
 
import UIKit
import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit

class PublishMessageViewController: UIViewController {

    @IBOutlet weak var myTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
 
        self.automaticallyAdjustsScrollViewInsets = false
        
        // Check if publish_actions permissions is granted
        if !(FBSDKAccessToken.current().hasGranted("publish_actions")) {
            
            print("Request publish_actions permissions")
            requestPublishPermissions()
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func publishMessage()
    {
        let messageToPost = myTextView.text
        
        if (messageToPost?.isEmpty)! {
           return
        }
        
        FBSDKGraphRequest.init(graphPath: "me/feed", parameters: ["message" : messageToPost!], httpMethod: "POST").start(completionHandler: { (connection, result, error) -> Void in
            if let error = error {
                print("Error: \(error)")
            } else {
                print("Success")
                self.myTextView.text = ""
            }
        })

    }
    
    func requestPublishPermissions()
    {
        let login: FBSDKLoginManager = FBSDKLoginManager()
        
        login.logIn(withPublishPermissions: ["publish_actions"], from: self) { (result, error) in
            if (error != nil) {
                print(error!)
            } else if (result?.isCancelled)! {
                print("Canceled")
            } else if (result?.grantedPermissions.contains("publish_actions"))! {
                print("permissions granted")
            }
        }
    }
    
     @IBAction func sendButton(_ sender: AnyObject) {
        if FBSDKAccessToken.current().hasGranted("publish_actions") {
            publishMessage()
        }
    }
    

}


Checkout more Swift code examples at Swift Code Examples page.


Learn iOS Development with these Video Courses

The Complete iOS 11 & Swift Developer Course - Build 20 Apps

Use Xcode 9 & Swift 4 to make real apps like Uber and Instagram, with CoreML & ARKit. Includes AWS Credit and much more. Preview this video course. icon

How to Make a Freaking iPhone App - iOS 11 and Swift 4

iPhone App Development from scratch. Learn how to make iOS apps using Xcode 9. The Basics Include Pokemon Go & Snapchat. Learn to build iOS Apps with Xcode 9 and Swift 4. How to Make a Freaking iPhone App - iOS 11 and Swift 4 icon

iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course

A Complete iOS 11 and Xcode 9 Course with Swift 4 & Objective-C. Preview this video course. iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course icon