Skip to main content

Facebook Login - Facebook Authentication and Cocoapods

Create a new application facebook login and authentication a facebook user using their iOS SDK with Cocoapods. Below are the steps in which we'll be following: 
First Getting Started guide from Facebook's iOS SDK Documentation 
Create a new iOS project Swift

New Swift Project Facebook Authentication

Open facebook Developer Account and create new App after go to setting and click basic -> add platform ios
Then add Bundle id your ios project like com.xxxx, Configuring Facebook with Bundle ID
Then go ahead Download facebook sdk for ios other wise install pod file your project.

Go ahead to open terminal and go ahead your project folder. 

Got create podfile -> pod init

Open podfile -> open podfile
Paste ->  pod 'FBSDKCoreKit' 
               pod 'FBSDKShareKit' 
               pod 'FBSDKLoginKit' podfile and save
Facebook SDK via CocoaPods

Then go to terminal or install podfiles -> pod Install

Enabling Facebook's Authentication Mechanism

In order for your app to behave nicely with Facebook's authentication mechanism, you need to edit your .plist file according to the facebook authentication Step 4: Configure Xcode Project of the Getting Started guide. Go ahead and replace the placeholder strings with your Facebook App Id and Application name:
  1. In Xcode, right-click your project's Info.plist file and select Open As -> Source Code. 
  2. Insert the following XML snippet into the body of your file just before the final </dict> element.
  3. Replace {your-app-id}, and {your-app-name} with your app's App's ID and name found on the Facebook App Dashboard.

Swift 3 AppDelegate Configurations 
Go to ahead swift project - AppDelegate.swift file. 
Import FBSDKCoreKit 
Next, FBSDKApplicationDelegate with in launchOptions 
like this afterwards:

import UIKit
import FBSDKCoreKit

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
        return handled


Integration  FBSDKLoginButton

Open up ViewController.swift and make the following source code edit
import UIKit
import FSocialLogin

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    override func viewDidLoad() {
        let loginButton = FBSDKLoginButton()
        //frame's are obselete, please use constraints instead because its 2016 after all
        loginButton.frame = CGRect(x: 16, y: 50, width: view.frame.width - 32, height: 50)
        loginButton.delegate = self

Output :-

Facebook Login Authentication

After click loginButton

You will be able to grant permission for your iOS application to know about this user:
Facebook Authentication permission
Keychain Sharing

In order to verify that the authentication is successful, you should implement the follow delegate methods and then set your button delegate:

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    override func viewDidLoad() {
        loginButton.delegate = self
    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("Did log out of facebook")
    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if error != nil {
        print("Successfully logged in with facebook...")

If your Facebook doesn't show a "Log out" label, the authentication was mostly unsuccessful. To fix this, enable Keychain Sharing in your application settings in Xcode:

Keychain Sharing

Run your application and perform the Log in authentication again. Now your button "Log out" label:
output: -

Facebook Login - Facebook Authentication and Cocoapods

Facebook Login - Facebook Authentication and Cocoapods


Popular posts from this blog

Add a Scene Delegate to your existing project with Storyboard in Swift

To add a scene delegate, first, create a new Swift file that you’ll call "SceneDelegate" containing a subclass of UIResponder, just like the AppDelegate, and that conforms to UIWindowSceneDelegate.  As your app might supports other versions than iOS 13, make this class only available for iOS 13. This is what you should have : If you are working a project that is storyboard based, please set storyboard  initial view controller SceneDelegate.swift import UIKit @available ( iOS 13.0 , *) class SceneDelegate : UIResponder , UIWindowSceneDelegate {     var window : UIWindow ?     func scene ( _ scene: UIScene , willConnectTo session: UISceneSession , options connectionOptions: UIScene . ConnectionOptions ) {                  let storyboard = UIStoryboard (name: "Main" , bundle: nil )         let initialViewController = storyboard. instantiateViewController (withIdentifier: "ViewController" )         let mainNavigationController = UINavigationControlle

How Create Animated Circle Progress Bar iOS 11 Swift 4

  Animated Circle Progress Bar iOS 11 Swift 4 With MBCircularProgressBa r - A circular, animatable & highly customizable progress bar from the Interface Builder Swift, Using pod fite MBCircularProgressBar Installation Cocoapods terminal. pod "MBCircularProgressBar" That - A Simple Steps to installed pod file -        Open terminal        Command on terminal go to project folder Cd path        set your project path on terminal.        command : pod init        open pod file - open -e podfile        added in pod file with in : pod "MBCircularProgressBar"        Command : Pod install        Close project of Xcode        open your Project from terminals        Command : open PodDemos.xcworkspace After opern StoryBoard and Now drag a UIView over the viewController in storyboard Or set UIView Constraint width, height or verticle or horzentail space and set a class MBCircul

How to Use Multiple Sections in UITableView iOS Swift !

Multiple sections in UITableView iOS Swift. UITableView is very important part of iOS ecosystem. So we split tableviews in sections. Then its easier to find right information.  1. First let’s create a project as usual. Create a new single view application X code project. Set project name to UIViewController.  2. Go to main storyboard & select view controller & use UITableView 3. Select tableview & make it initial view controller  4 Create a custom Sections Class like Name => TableSections, create register cell static return “ getCellNibs ” method. Then create  4 section enum “TableItems” then after append all sections to an array model. import UIKit struct CellNib {      static func getCellNibs () -> [ String ] {          return [ "Cell1" , "Cell2" , "Cell3" , "Cell4" ]     } } enum TableItems : Int {      case TableSections1      case TableSections2      case TableSections3      case TableSections4 } class TableSec