Thursday, April 28, 2016

Facebook SDK and Swift - Create a custom login button programmatically

The FBSDKLoginButton style is like this:


To create a custom Facebook login button, follow these two tutorials first:

Facebook SDK and Swift - Create a Facebook Login Button
Facebook SDK and Swift - Display User Name and Profile Picture

Then remove the FBSDKLoginButton and create a UIButton instead. Add this to viewDidLoad()with Xcode 7.3 (Swift 2.2):


let btnSize : CGFloat = 100
let btnLogin = UIButton(frame: CGRectMake(0,0,btnSize,btnSize))
btnLogin.center = CGPoint(x: view.center.x, y: 400)
btnLogin.setImage(UIImage(named: "facebook_logo.png"), forState: UIControlState.Normal)
btnLogin.addTarget(self, action: #selector(btnLoginPressed), forControlEvents: UIControlEvents.TouchUpInside)
        
//Circular button
btnLogin.layer.cornerRadius = btnSize/2
btnLogin.layer.masksToBounds = true
btnLogin.layer.borderColor = UIColor.blackColor().CGColor
btnLogin.layer.borderWidth = 2

view.addSubview(btnLogin)

and then add this:

func btnLoginPressed() {

let loginManager = FBSDKLoginManager()
loginManager.logInWithReadPermissions(["public_profile"], fromViewController: self, handler: { (response:FBSDKLoginManagerLoginResult!, error: NSError!) in
    if(error == nil){
        print("No Error")
        self.getFacebookUserInfo()
        }
    })
}

Result:


Related Information:

Google Sign-In for iOS - Create a custom sign-in button programmatically

No comments:

Post a Comment