0
votes

I'm trying to implement facebook login on IOS using the new Facebook SDK. I'm getting stuck because I'm using my own custom facebook login button, where the SDK requires you to use its own login button. Can someone please help? I need to implement the part where someone click on the custom login button and authenticates using facebook after granting permission. Thanks!

I looked at the Documentation and it only provides support for Objective C and not swift

Here's what I had before the SDK update that no longer works

  let loginManager = LoginManager()
    loginManager.logIn(permissions: [ ".publicProfile, .email" ], viewController: self) { loginResult in
        switch loginResult {
        case .failed:
            
            self.showMessage("Error logging you in.", "Could not log you in. Please try again.", .error)
        case .cancelled:
            self.showMessage("User canceled login.", "User cancelled login. Please try again.", .error)
        case .success:
            self.setFBUserData()
        }
    }
2
It would be very helpful if you please provide some code based on this issue.Faysal Ahmed
Provided. See codePota Onasys

2 Answers

1
votes

You can do this tricks. Initially create a facebookLoginButton but this will be hidden.

let facebookLoginButton = FBLoginButton(frame: .zero, permissions: [.publicProfile, .email])
facebookLoginButton.delegate = self
facebookLoginButton.isHidden = true

and in your custom button action you just needed to process the action

@IBAction func registerWithFacebook(_ sender: UIButton) {
    facebookLoginButton.sendActions(for: .touchUpInside)
}

Hope you can understand.

About this trick you can check more: https://medium.com/@emmavagradyan/handle-facebook-login-with-custom-button-in-ios-3f1c99faeb55

Configuration about FacebookLogin: https://itnext.io/swift-facebook-ios-login-sdk-dada412d8341

0
votes

For the Custom Design I did this way. First create a customView For the Custom Button and add Tap Gesture to that View.

create outlet of your view

@IBOutlet weak var fbLoginContainerView: UIView!

then add tap gesture to that view

let fbContainerTapGesture = UITapGestureRecognizer(target: self, action: #selector(fbLoginVIewAction(_:)))
fbLoginContainerView.addGestureRecognizer(fbContainerTapGesture)

in action getFBUserData

 @objc func fbLoginVIewAction(_ sender: UITapGestureRecognizer) {
        
        let loginManager = LoginManager()
        loginManager.logIn(permissions: ["public_profile", "email"] , viewController: self) { loginResult in
            switch loginResult {
            case .failed(let error):
                print(error)
            case .cancelled:
                print("User cancelled login.")
            case .success(let grantedPermissions, let declinedPermissions, let accessToken):
                self.getFBUserData()
            }
        }
        
    }



fileprivate func getFBUserData(){
        if((AccessToken.current) != nil){
            GraphRequest(graphPath: "me", parameters: ["fields": "id, name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
                if (error == nil){
                    self.dict = result as? [String : AnyObject]
                    print(result!)
                    print(self.dict)
                    self.showProgress(on: self.view)

                    guard let Username = self.dict["name"]! as? String else { return }
                    guard let UserId = self.dict["id"]! as? String else { return }
                    guard let Useremail = self.dict["email"]! as? String else { return }
                    let params = ["provider_user_id": UserId , "name": Username, "email": Useremail ,"provider": "FacebookProvider"] as! [String: AnyObject]
                    //Do your sign in network call here with parameter
                   
        }
    }

See the Button ScreenShot enter image description here