Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FacebookSDK(4.1.x) Custom Login UI Button - Swift(1.2)

Following this tutorial, I have managed to make a Facebook Login Button working. However, it is assigning the button image automatically from the SDK and it is not customisable (as it is not getting created on Storyboard), so I am unable to use my own button image or text instead.

I believe this part of the code (in ViewDidLoad) is assigning the button:

        let loginView : FBSDKLoginButton = FBSDKLoginButton()
        self.view.addSubview(loginView)
        loginView.center = self.view.center
        loginView.readPermissions = ["public_profile", "email"]
        loginView.delegate = self

What I need to do is creating a @IBOutlet Button on Storyboard and customise it from there. How can I do that?

like image 726
senty Avatar asked Jun 04 '15 03:06

senty


2 Answers

Login with custom button and access token.

Get user info in facebook sdk 4.x

Swift

@IBAction func btnFBLoginPressed(sender: AnyObject) {
    var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -> Void in
        if (error == nil){
            var fbloginresult : FBSDKLoginManagerLoginResult = result
            if(fbloginresult.grantedPermissions.contains("email"))
            {
                self.getFBUserData()
                fbLoginManager.logOut()
            }
        }
    })
}

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){
                println(result)
            }
        })
    }
}
like image 171
Dharmesh Dhorajiya Avatar answered Nov 22 '22 01:11

Dharmesh Dhorajiya


Updated answer using the last Facebook SDK (01/05/2016) and Swift 2.1

Create a UIButton with Interface Builder or by code and link the action of that button with this:

@IBAction func loginFacebookAction(sender: AnyObject) {
    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()
            }
        }
    }
}

The happy case of the previous code triggers the function self.getFBUserData() so you have to implement that function in the same file

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)
            }
        })
    }
}
like image 40
Santos Ramón Avatar answered Nov 22 '22 03:11

Santos Ramón