Facebook Integration using Swift

     Objective : The main purpose of this post   to learn facebook integrate in swift
     OUTPUT  : If  I Integrate Facebook In My Application Then I will Get Following Features
  •  I Can Directly Login to Facebook Through My IOS Application
  •  I Can Directly LogOut to Facebook Through My IOS Application
  •  I Can Get  All Details Of User

    1.Create a new project

    2.Create  facebook appID and Get AppID       

     -> Go to  developer page  https://developers.facebook.com/

            If You Alredy Created facebook appID Then Ignore The Step 2   Just Click On  Create New App

            -> Login into Your Facebook Acount

                      -> Go to My Apps.

                      -> To create new Facebook app, click on “Add a New App button.

                      -> Select a Platform:iOS.

                      –Give appropriate name of the App and click on Create New Facebook App ID.

                       ->  Choose your Category and click on Create App.

                      -> Enter Your Project Bundle Identifire To Your  Bundle Identifire TextField

           -> Click To NEXT

                                                                    In this way your Facebook app has been created.!!!!!

3.Add FBSDKLoginKit & FBSDKCoreKit Frameworks

             -> Download The latest FaceBook SDK provided by Facebook itself  Facebook SDK version

             -> Drag The  FBSDKLoginKit.framework  AND  FBSDKCoreKit.framework  In Your Project

                                             Tips: Make sure “Copy items if needed is checked”.

4.    open Your Info.plist With Source Code  Click Right To Info.plist   

                >     Add This Code To Your Source Code File ·
                            <key>CFBundleURLTypes</key>

                               <array>

                                          <dict>

                                                   <key>CFBundleURLSchemes</key>

                                        <array>

                                                <string>fb+”YOUR Facebook APP ID”</string>

                                            </array>

                                   </dict>

                            </array>

                          <key>FacebookAppID</key>

                           <string>”YOUR Facebook APP ID”</string>

                    <key>FacebookDisplayName</key>

                     <string> Name  </string>

5.How We can Use Objective File In Swift (how To Create Bride File)                            

  • Click On Your Project, Create a new header file and give name “FileName.h”
  •  select build settings -> search for Swift Compiler – General
  • Select Objective-C Bridging Header item and double click on it from right side
  • Now Copy This Content And Paste In The PopUp View  $(PROJECT_DIR)/$(PROJECT_NAME)/$(PROJECT_NAME)-Bridging-Header.h
  • Click outside for saving , Now you can import your Objective-C libraries (eg. #import “ViewController.h”) to bridging file and start using it in your swift project.
  • Import the Following File
                     #import <FBSDKCoreKit/FBSDKCoreKit.h>
                     #import <FBSDKLoginKit/FBSDKLoginKit.h>

 6. Open AppDelegate.swift File

  • To conform facebook with your Apllication And Login SuccessFully

                                func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:[NSObject: AnyObject]?) -> Bool                     {                    return FBSDKApplicationDelegate.sharedInstance().application(application,                                                            didFinishLaunchingWithOptions: launchOptions)

                       }

  • This Method is For Sign-Out If User Terminate Application

                              func applicationWillTerminate(application: UIApplication) {    

                                   let loginManager: FBSDKLoginManager = FBSDKLoginManager()    

                                   loginManager.logOut()     

                        }

  1.         Design User Interface  

                                 Simulator Screen Shot 09-Sep-2017, 2.12.08 PM                   

 

  • Set “FBSDKLoginButton” as a custom class of UIButton.
  • Create IBOutlet Of Button,Uiimage And Label

  8.   Configure Facebook Login

  • In ViewController.Swift

                           func configureFacebook(){

                                                      btnFacebook.readPermissions = [“public_profile”, “email”, “user_friends”];    

                                                      btnFacebook.delegate = self

                                                   }

  9. Import FBSDKLoginButtonDelegate Method

                                  import UIKitclass ViewController: UIViewController, FBSDKLoginButtonDelegate

  • First Method

               func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!){}

  • Second Method

                func loginButtonDidLogOut(loginButton: FBSDKLoginButton!){}    

10.Get The User Profile Data On Project

                            here ,we are fetching Three Data

  •  FirstName
  •   LastName
  •   ProfilePicture                       

             Fetching The Data Using This Method

                func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!){

                FBSDKGraphRequest.init(graphPath: “me”, parameters: [“fields”:”first_name, last_name,            

                         picture.type(large)”]).startWithCompletionHandler

                  {(connection, result, error) -> Void in         

                  let strFirstName: String = (result.objectForKey(“first_name”) as? String)!    

                   let strLastName: String = (result.objectForKey(“last_name”) as? String)!    

                   let strPictureURL: String = (result.objectForKey(“picture”)?.objectForKey(“data”)?.objectForKey(“url”) as? String)!  

                   self.lblName.text = “Welcome, \(strFirstName) \(strLastName)”    

                  self.ivUserProfileImage.image = UIImage(data:              

                   NSData(contentsOfURL: NSURL(string: strPictureURL)!)!)  

               }

        }

 11.FaceBook LogOut 

 func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)  { 

                              let loginManager: FBSDKLoginManager = FBSDKLoginManager()    

                              loginManager.logOut()       

                               ivUserProfileImage.image = nil       

                              lblName.text = “”

                    }

 12. Build And Run the Demo

  • Now click on “Log in with Facebook” button Enter your Facebook credential and Allow permission for this application

                                                         !!!!!!!!!!!!!!!!!!!!! THANK YOU !!!!!!!!!!!!!!!!!!!

 

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *