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.

[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.