带有FBSDKLoginButton图像的自定义按钮

时间:2016-03-30 20:17:39

标签: xcode swift facebook swift2

我想使用带图片的自定义按钮

我不想使用Facebook默认登录按钮。

这是我的代码

    let loginButton = FBSDKLoginButton()
    loginButton.readPermissions = ["public_profile", "email", "user_friends"]
    loginButton.center = self.view.center
    loginButton.delegate = self
    loginButton.imageView?.image = UIImage(named: "cyanx2")
    self.view.addSubview(loginButton)

我的按钮是

@IBAction func customButton(sender: UIButton) {

 // here's go the action when click on default facebook button   
}

1 个答案:

答案 0 :(得分:0)

根据this帖子

我解决了我的问题^ _ ^这就是代码

按钮功能

@IBAction func loginFacebookAction(sender: AnyObject) {
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in

        if error != nil {
            NSLog("Process error")
        }
        else if result.isCancelled {
            NSLog("Cancelled")
        }
        else {
            NSLog("Logged in")
            self.getFBUserData()
        }

    }
}

从facebook

获取结果的功能
 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)
                }
            })
        }
    }