Social login in IOS using Swfit

2.1k views Asked by At

Well this is general question, what I want that If anyone login on my using Google-Auth/Email-Password login and I can gets it Facebook profile info's too? Is these steps are correct:

  1. Login with google and insert the entry in Firebase.
  2. On Profile Edit there is button named "Get Your Facebook Data".
  3. On clicking button with the help of FBSDKLoginManager I connect with user fb's profile and make FBSDKGraphRequest.
  4. After getting result save it to database.

Suggest any other way if its possible?!

1

There are 1 answers

1
Shobhakar Tiwari On

Here is Working code of Xcode 8.3 along with Swift 3.1 :

Follow these steps to get Facebook data :-

Step 1:

import FBSDKCoreKit

import FBSDKLoginKit

Step 2: Conforms delegate .

FBSDKLoginButtonDelegate

Step 3: Login Button IBAction Method .

//Facebook Login Action

  @IBAction func facebookLoginAction(_ sender: Any) {

        //Fb login process
        let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.loginBehavior = .native

        fbLoginManager.logOut()
        fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
            if (error == nil){
                let fbloginresult : FBSDKLoginManagerLoginResult = result!
                if fbloginresult.isCancelled {

                }
                else if (fbloginresult.declinedPermissions != nil){
                    if(fbloginresult.grantedPermissions.contains("email")) {
                        self.getFBUserData()
                    }
                }

            }
        }

    }

Step 4: Get Facebook Data following way .

  func getFBUserData(){
        if((FBSDKAccessToken.current()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name, last_name,email, picture.type(large),gender"]).start(completionHandler: { (connection, result, error) -> Void in
                if (error == nil){
                    let newResult = result as AnyObject
                    let email = newResult["email"] as? String ?? ""
                    print("Your Email = \(email)")
                    let firstName = newResult["first_name"] as? String ?? ""
                    let lastName  = newResult["last_name"] as? String ?? ""
                    print(FBSDKAccessToken.current())
                    let gender = newResult["gender"] as? String ?? ""
                    let id = newResult["id"] as? String ?? ""
                    print("the access token is \(FBSDKAccessToken.current().tokenString)")

                    let accessToken = FBSDKAccessToken.current().tokenString
                    //self.signInUser()

                }
            })
        }
    }

Feel free to comment on this , Hope it solve your issue. Thanks