Facebook iOS Tutorial Using Swift 3 + Xcode 8

Facebook iOS Tutorial Using Swift 3 + Xcode 8

Hi, In this post, I'll show you how to use Social Framework in iOS applications. Using the Social Framework, you can easily integrate the services of any supported social network in your iOS apps. Its very easy to implement and you can easily integrate social networks like Facebook, Twitter etc. in your iOS apps.

In this tutorial, We'll use Social framework to create a post from our app and send it to Facebook feeds wall. So, let's start and see how to do it.. :)

First of all, open your Xcode and create a Single View Application. Click Next.


Give some name to your product and fill other details as shown in the image below. Make sure to choose swift as the language and click Next.


Now, go to Main.storyboard and drag a button from object library and give it some title like "Post To Facebook". 

Its time to add some constraints on the button. So, select your button and click on the Align Auto Layout button on the bottom-right of your storyboard. Select the "Horizontally in Container" option and click on "Add 1 constraint". Check below image for your reference.


Similarly, click on the Pin Layout button and pin the button from top and click on Add 1 Constraint.


Once this done, its time to get into the code. For that, first connect this button to your viewcontroller.swift file. So, select the Assistant Editor and make sure both storyboard and controller file is visible as shown below. 



Now, ctrl-drag the button to your viewcontroller.swift file and create its action.


Once this done, open your viewcontroller.swift file and import the Social framework.

import Social                                                                  

Now add the following code under postFBBtn() method.

@IBAction func postFBBtn(_ sender: Any) {
        
        //first check if any Facebook account available on device
        if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook){
            
            //create a SLComposeViewController object, which will add the Facebook post Compose view and all other functionality.
            
            if let controller = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            {
                //present the Facebook post view controller
                self.present(controller, animated: true, completion: nil)
            }
        
        } else {
            //we'll display a message at the console if the Facebook account not available on device
            
            print("No Facebook account found on this device.")
        }
        
    }

That's it. Build and run your app and start posting to Facebook right away from your own newly created social framework app. Make sure a Facebook account is available in the device on which you're testing it. You can check for this in your iPhone Settings> Facebook

So, whenever you click on Post To Facebook button on your app it will open this view for creating your Facebook post.


Looking nice, right? 

So, that's all about this post. Here's the full code for your reference:

________________________________________________________________

import UIKit

import Social

class ViewController: UIViewController {
    
    @IBAction func postFBBtn(_ sender: Any) {
        
        //first check if any Facebook account available on device
        if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook){
            
            //create a SLComposeViewController object, which will add the Facebook Post Compose view and all other functionality.
            
            if let controller = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            {
                //present the Facebook post view controller
                self.present(controller, animated: true, completion: nil)
            }
        
        } else {
            //we'll display a message at the console if the Facebook account not available on device
            
            print("No Facebook account found on this device.")
        }
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

}
_________________________________________________________________________________


I hope this post may help someone, somehow! :)

If you've any doubts or need any further help, do message me in comments section below and I'll try to respond you as soon as possible. 

Thanks for your time! Keep Coding..

Comments

Popular posts from this blog

How To Make The Device Vibrate Or Ring- An iOS Swift Tutorial

PayPal Integration In iOS Applications | Payment Gateways iOS/Swift

How To Parse JSON Data In iOS (Swift 3 + Xcode 8) ?