Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to login with Facebook in swift 3.0

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 790
    Comment on it

    To Implement Facebook login in swift 3.0, we can use following steps.

    1. install pod

     pod 'FacebookCore'
     pod 'FacebookLogin'

    2.Register your app on the Facebook  developer account. After registration,Go to you info.plist, right click on it and select “Open As” and click “Source code

    3.Now add your fb id in “CFBundleURLSchemes” with “fb” prefix.

    <dict>
     <key>CFBundleURLSchemes</key>
      <array>
         <string>fb + Your FB id</string>
      </array>
    </dict> 

    4.In your “LSApplicationQueriesSchemes” add these string

    <key>LSApplicationQueriesSchemes</key>
      <array>
    	<string>fbapi</string>
    	<string>fb-messenger-api</string>
    	<string>fbauth2</string>
      </array>

    5.Now add your fb id in “FacebookAppID” key

    <key>FacebookAppID</key>
       <string>your fb id</string>

    6. After that, do following code in AppDelegate file of your project  -

    i). Import facebookcore kit

    import FacebookCore

    ii). In you didFinishLaunchingWithOption function add following code

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            
            // Override point for customization after application launch.
            SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
            return true
        }

    iii). Add this function in your app delegate

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
            
            return SDKApplicationDelegate.shared.application(app, open: url, sourceApplication: (options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String), annotation: options[UIApplicationOpenURLOptionsKey.annotation] as Any)
    
    }

    7. Now Create the Facebook manager Class which will manage the Facebook Sign In Process and write the following code in the class

    import Foundation
    import FacebookLogin
    import FacebookCore
    
    struct FBManager {
        
        // Login With Facebook
        func fbLogin(viewController:UIViewController, completion:@escaping (_ responseObject:AnyObject?, _ error:NSError?, _ success:Bool)->()) {
            let loginManager = LoginManager()
            loginManager.logOut()
            loginManager.logIn([.publicProfile, .email], viewController: viewController) { loginResult in
    
                switch loginResult{
                    
                case .cancelled:
                    completion(nil,nil,false)
                    
                case .failed(let error):
                    completion(nil,error as NSError?,false)
                     
                case .success(let grantedPermission,  _,  _):
                    if grantedPermission.contains("email"){
                        print("Logged in!")
    
                        let params = ["fields" : "first_name, last_name, email, picture.type(large), id, name"]
                        let graphRequest = GraphRequest(graphPath: "me", parameters: params)
                        graphRequest.start {
                            (urlResponse, requestResult) in
    
                            switch requestResult {
                            case .success(let response):
                                print("Custom Graph Request Succeeded: \(response)")
                                print("My facebook id is \(response.dictionaryValue?["id"])")
                                completion(response.dictionaryValue as AnyObject?,nil,true)
                            
                            case .failed(let error):
                                print("Custom Graph Request Failed: \(error)")
                                completion(nil,error as NSError,false)
                            }
                        }
                    }
                    
                }
            }
        }
        
    }
    

    8. We can call this function from our view controller like this:

    let fbManage = FBManager()
            fbManage.fbLogin(viewController: self, completion: { (result, error, status) in
                if status{
                    //Logged In Successfully"
                }
                else{
                    if error?.description != nil{
                        // Logged In Failed
                    }
                }
            })

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: