如何将FBSDKLoginButton默认图像添加到自定义UIButton?

时间:2015-05-17 13:08:38

标签: ios objective-c facebook parse-platform uibutton

我想将Parse与Facebook整合。为此,我需要在任何我想要的地方调用PFFacebookUtils logInInBackgroundWithReadPermissions方法。在整合它们的解析指南中,它告诉调用此方法,Parse和Facebook帐户将被链接,仅此而已。当我在内部触摸UIButton实例并且一切正常时,我正在调用它,但我想知道如何添加FBSDKLoginButton(简单行为按钮而不连接用户,只是将它们验证到Facebook)默认图像到我的自定义按钮。

2 个答案:

答案 0 :(得分:2)

我发现了一个问题: 如何登录解析(创建PFUser以使用所有舒适的Parse东西),使用FBSDKLoginbutton设计和强大的功能。 希望这是你问的问题。 我的解决方案 使用您的按钮作为FBSDKLoginButton(更改身份检查器中按钮的类), 将FBSDKLoginButtonDelegate添加到您的班级

        @IBOutlet var fbLoginButton: FBSDKLoginButton!

class ViewController: UIViewController , FBSDKLoginButtonDelegate  {
override func viewDidLoad() {
    super.viewDidLoad()
    self.fbLoginButton.readPermissions = ["public_profile","email", "user_friends" ]
    self.fbLoginButton.delegate = self
}

Xcode会咆哮你,因为你需要清除它应该做什么,当它登录并注销时:

      //login to Facebook  
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
    if let token = result.token{

//login to Parse with token
        PFFacebookUtils.logInInBackgroundWithAccessToken(token, block: { (user: PFUser?, error: NSError?) -> Void in
            if error != nil {
                //process error
                print(error?.localizedDescription)
                return
            }
            else
            {
                //here you have your PFUser
            }
})
}
}

不要忘记委派的注销说明:

        func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
    PFUser.logOutInBackground()
}

答案 1 :(得分:0)

如果我理解正确,您正在考虑创建自己的FBSDKLoginButton版本,该版本可以使用Parse PFFacebookUtils

我最近有同样的必要性,我使用了以下方法:

  1. 创建UIButton - customUIButton
  2. 创建FBSDKLoginButton - fbSDKLoginButton
  3. 设置fbSDKLoginButton.userInteractionEnabled = false
  4. fbSDKLoginButton添加为customUIButton
  5. 的子视图
  6. 将您需要的操作添加到customUIButton以通过Parse自行处理登录 - FBSDKLoginButton将是"透明"用户触摸但在收到SDK的登录/注销通知时会不断更新。
  7. 这是我在Swift中的示例代码:

    import UIKit
    import FBSDKLoginKit
    import Parse
    
    class CustomFBLoginButton: UIButton
    {
        init() {
            super.init(frame: CGRectZero)
            let fbSDKLoginButton = FBSDKLoginButton()
            fbSDKLoginButton.userInteractionEnabled = false//so it won't respond to touches
            self.addSubview(fbSDKLoginButton)
            self.frame = fbSDKLoginButton.frame//FBSDKLoginButton gets automatically the right frame - so let's adjust its superview to have exactly the same frame
        }
    
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    }
    
    class ViewController: UIViewController
    {
        override func viewDidLoad() {
            super.viewDidLoad()
            let customFBLoginButton = CustomFBLoginButton()
            customFBLoginButton.addTarget(self, action: "customButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
            view.addSubview(customFBLoginButton)
        }
    
        func customButtonTapped(button: CustomFBLoginButton)
        {
            //handle login/logout via the Parse SDK
            if PFUser.currentUser() != nil {
                PFUser.logOutInBackgroundWithBlock({ (_) -> Void in
                    println("logged out")//the button will now refresh although I noticed a small delay 1/2 seconds
                })
            }
            else {
                PFFacebookUtils.logInInBackgroundWithReadPermissions(nil)//set the permissions you need
            }
        }
    }