Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Login button for Swift

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!

like image 793
Soporificdreamer Avatar asked Apr 20 '15 14:04

Soporificdreamer


People also ask

How do I make a login button in Swift?

First, add a button and label into your view controller. Create 2 IBOutlets loginButton and messageLabel and connect them to the label and button you just added. Next, add a “touch up inside” IBAction named loginButtonTapped(_:) and connect it to the loginButton .

What is the log back in button on Facebook?

The Login button is a simple way to trigger the Facebook Login process on your website or web app. If someone hasn't logged into your app yet, they'll see this button, and clicking it will open a Login dialog, starting the login flow.


2 Answers

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])
    }
like image 82
Talha Rasool Avatar answered Oct 02 '22 19:10

Talha Rasool


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.

like image 39
veducm Avatar answered Oct 02 '22 19:10

veducm