Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customized Facebook Login Button - After integration

I am trying to customize the Facebook login button after following this guide. Everything is integrated correctly, I can login to Facebook, and logout without a hitch.

Now my next goal is to make the screen look like my mock up, this includes placing the login button correctly on the storyboard, but the only issue is that it doesn't show up on the story board, it just appears when I run the simulation. I looked at other overflow answers, but they weren't really helpful as they were geared towards earlier versions of swift / Xcode, and didn't work from what the comments said. My code is exactly the same as the guide, as this is the first screen that I am trying to implement.

Help would be much appreciated.

like image 431
Jordan Benge Avatar asked Apr 03 '16 00:04

Jordan Benge


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.


1 Answers

If you want to use the custom facebook SDK button you can create any custom button in the storyboard and provide action in the viewcontroller like this

Import at top

import FBSDKCoreKit
import FBSDKLoginKit

Swift 3:

  @IBAction func loginFacebookAction(sender: AnyObject) {//action of the custom button in the storyboard
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
      if (error == nil){
        let fbloginresult : FBSDKLoginManagerLoginResult = result!
        // if user cancel the login 
        if (result?.isCancelled)!{
                return
        }
        if(fbloginresult.grantedPermissions.contains("email"))
        {
          self.getFBUserData()
        }
      }
    }
  }

  func getFBUserData(){
    if((FBSDKAccessToken.current()) != nil){
      FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
        if (error == nil){
          //everything works print the user data
          print(result)
        }
      })
    }
  }

Older Swift version:

 @IBAction func loginFacebookAction(sender: AnyObject) {//action of the custom button in the storyboard
        let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
            if (error == nil){
                let fbloginresult : FBSDKLoginManagerLoginResult = result
                if(fbloginresult.grantedPermissions.contains("email"))
                {
                    self.getFBUserData()
                }
            }
        }
    }

    func getFBUserData(){
        if((FBSDKAccessToken.currentAccessToken()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
                if (error == nil){
                    //everything works print the user data
                    print(result)
                }
            })
        }
    }



If you want default facebook login button you can take UIView and set Class* field of the Custom Class** section in the Identity Inspector, we must set the FBLoginValue

Ref: Default SDK login button tutorial:http://www.appcoda.com/ios-programming-facebook-login-sdk/

Ref: Custom SDK login button for parameters and more info:https://developers.facebook.com/docs/facebook-login/ios

like image 190
HardikDG Avatar answered Oct 20 '22 00:10

HardikDG