Facebook Login button for Swift

27,498

Solution 1

One option would be to set your view controller as a delegate of the FBSDKLoginButton and implement the loginButton:didCompleteWithResult:error: method, which is called when the button is used to login.

Swift

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    @IBOutlet weak var loginButton: FBSDKLoginButton!        

    override func viewDidLoad() {
        super.viewDidLoad()

        self.loginButton.delegate = self
    }
}

Obj-C

// ViewController.h
@interface ViewController : UIViewController <FBSDKLoginButtonDelegate>

@property (weak, nonatomic) IBOutlet FBSDKLoginButton *loginButton;

@end

// ViewController.m
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.loginButton.delegate = self;
}

Then, in the loginButton:didCompleteWithResult:error: method you can check the result and error, and if everything is fine, navigate to another view.

Swift

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        if ((error) != nil) {
            // Process error
        }
        else if result.isCancelled {
            // Handle cancellations
        }
        else {
            // Navigate to other view
        }   
    }

Obj-C

// ViewController.m
@implementation ViewController

- (void)loginButton:(FBSDKLoginButton *)loginButton 
  didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
                  error:(NSError *)error {
    if (error) {
        // Process error
    }
    else if (result.isCancelled) {
       // Handle cancellations
    }
    else {
        // Navigate to other view
    }
}

You can find more about how to login with FB in their docs.

Solution 2

In Swift that would be something like:

class MyViewController: UIViewController, FBSDKLoginButtonDelegate {
    @IBOutlet weak var loginView : FBSDKLoginButton!
    @IBOutlet weak var profilePictureView : FBSDKProfilePictureView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.loginView.delegate = self

        if (FBSDKAccessToken.currentAccessToken() != nil)
        {
            performSegueWithIdentifier("unwindToViewOtherController", sender: self) 
        }
        else
        {
            loginView.readPermissions = ["public_profile", "email", "user_friends"]
        }

    }

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        println("User Logged In")

        if ((error) != nil)
        {
            // Process error
        }
        else if result.isCancelled {
            // Handle cancellations
        }
        else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if result.grantedPermissions.contains("email")
            {
                // Do work
            }
        }
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        println("User Logged Out")
    }
}

Then in your TargetViewController add an unwind function:

@IBAction func unwindToViewOtherController(segue:UIStoryboardSegue) {
    }

Solution 3

In current FacebookLogin version (0.2.0) for Swift, the LoginButton delegate property is defined as a strong property:

public class LoginButton: UIView {
...
  /// Delegate of the login button that can handle the result, logout events.
public var delegate: LoginButtonDelegate?
... }

If you add the login button following Facebook instructions and you set your UIViewController child class as button delegate...

import FacebookLogin

func viewDidLoad() {
    let loginButton = LoginButton(readPermissions: [ .PublicProfile ])
    loginButton.center = view.center
    loginButton.delegate = self
    view.addSubview(loginButton)
}

... a reference cycle will be created. The view will contain a strong reference to the button, the button will contain a strong reference to the controller, and the controller will have a strong reference to its view, see this post.

My solution was to use a weak member variable to have a reference to the login button and when the view disappears, the button delegate is set to nil, like this:

import UIKit
import FacebookCore
import FacebookLogin
import RxSwift

class LoginViewController: UIViewController, LoginButtonDelegate {

    private weak var facebookLoginButton: LoginButton? = nil

    override func viewDidLoad() {

        super.viewDidLoad()

        // Add the Facebook login button
        let loginButton = LoginButton(readPermissions: [ .publicProfile, .email, .userFriends ])
        loginButton.center = view.center
        // WARNING!: Facebook login button delegate property is defined currently as STRONG.
        // Therefore, it must be set to nil before leaving the view to avoid reference cycles
        loginButton.delegate = self
        view.addSubview(loginButton)
        // Store the login button as a weak reference, since it is holded by the main view with a
        // strong reference
        facebookLoginButton = loginButton
    }

    override func willMove(toParentViewController parent: UIViewController?) {
        super.willMove(toParentViewController:parent)
        if parent == nil {
            // The back button was pressed, interactive gesture used, or programatically pop view
            // was executed
            // Do not forget to set delegate in Facebook button to nil to break reference cycle.
            facebookLoginButton?.delegate = nil
        }
    }

    // MARK: - Facebook login

    /**
     Called when the button was used to login and the process finished.

     - parameter loginButton: Button that was used to login.
     - parameter result:      The result of the login.
     */
    func loginButtonDidCompleteLogin(_ loginButton: LoginButton, result: LoginResult) {

        switch result {
            case .failed(let error):
                // Action on failed
            case .cancelled:
                // Action on cancelled
            case .success(let grantedPermissions, let declinedPermissions, let accessToken):
                // Action on success
        }
    }

    /**
     Called when the button was used to logout.

     - parameter loginButton: Button that was used to logout.
     */
    func loginButtonDidLogOut(_ loginButton: LoginButton) {

        // Action on logout
    }
}

Do not use function viewWillDissapear() for setting to nil the delegate, because Facebook login page will be shown on top of your app, triggering this function, and you will not get the login result since you will not be the delegate anymore. Note that this solution is working fine for views inside a navigation controller. Another solution should be found for modal windows.

I hope it helps, Xavi

Solution 4

IOS 13 use Scene Delegate. Just paste the below code in scene delegate and simple call the facebook login manager it will return the user object of facebook. This function automatically call.

 func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let url = URLContexts.first?.url else {
            return
        }
        let _ = ApplicationDelegate.shared.application(
            UIApplication.shared,
            open: url,
            sourceApplication: nil,
            annotation: [UIApplication.OpenURLOptionsKey.annotation])
    }
Share:
27,498

Related videos on Youtube

Soporificdreamer
Author by

Soporificdreamer

Updated on July 01, 2022

Comments

  • Soporificdreamer
    Soporificdreamer almost 2 years

    In Xcode if I create a UIView and then add the custom class as FBSDKLoginButton, when I click it leads me through the Facebook login and then returns me to the same page as the FBSDKLoginButton but instead of saying login button it says log out now. How would I go about making that when the login button is clicked it lead to a new view?


    I downloaded the Facebook SDK through cocoapods and its my first time working with it so I am confused about this. Thanks for the help!

  • J Richard Snape
    J Richard Snape about 9 years
    This came to me in a review queue as it was marked as a Link only answer, which are generally not seen as good on here.. I can see you've responded to the comment about link only answers. I edited to tidy up a bit of formatting / typos.
  • Nate Uni
    Nate Uni almost 9 years
    Thanks bud.. a snippet of that proved handy!
  • rAzOr
    rAzOr almost 9 years
    performSegueWithIdentifier("unwindToViewOtherController", sender: self) inside if (FBSDKAccessToken.currentAccessToken() != nil) isn't loading next viewcontroller. Would u please help me with this??
  • Ruud Kalis
    Ruud Kalis almost 9 years
    In the storyboard select the unwind segue and then the attribute inspector. There you should have unwindToViewOtherController as Identifier and unwindToViewOtherController: as Action.
  • rAzOr
    rAzOr almost 9 years
    I had to add viewDidAppear function and write performSegueWithIdentifier in it. That solved the issue. We can't have performSegueWithIdentifier inside viewDidLoad function, that's what I learned from google. Would you please help me know why?
  • Ruud Kalis
    Ruud Kalis almost 9 years
    You cannot dismiss a view that has not loaded yet. ViewDidLoad it is not after this function is finished that the view is presented and thus can be dismissed.
  • GhostCat
    GhostCat over 6 years
    If you drop the lines saying "this is a comment" ... this would actually look like an answer to me ;-)