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
https://developers.facebook.com/docs/ios/getting-started
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
Like 


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

@UIApplicationMain
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() {
        super.viewDidLoad()
        
        let loginButton = FBSDKLoginButton()
        view.addSubview(loginButton)
        //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:

ViewController.swift
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(error)
            return
        }
        
        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

Comments

Popular posts from this blog

How Create Animated Circle Progress Bar iOS 11 Swift 4

Animated Circle Progress Bar iOS 11 Swift 4


With MBCircularProgressBar - https://github.com/MatiBot/MBCircularProgressBar


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 MBCircularProgressBarView in StoryBoard. Open Atribute inspector and cu…

Core Data With Swift 4.0 Tutorial

Core Data - Core Data is a framework that you use to manage the data model layer objects or instance Context in your application. It provides generalized and automated data stores solutions to common tasks associated with object life cycle and object graph management, including persistence.
Completely Tutorial for Swift 4 and iOS 11. Gating Start Goto Xcode and create New iOS Project on single view. Project Name CoreDataSwift or Checked Use Core Data

On click Main.Storyboard in Interface builder click on project navigation Editor - Embed In - Navigation controller.



Then After click on Navigation ViewController and next Drag TableView in to ViewController.


Make TableView viewcontroller Outlet Delegate, DataSource and import CoreData

import UIKit import CoreData
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { overridefunc viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } overridefunc didReceiveMem…