2015-05-17 114 views
-1

我想将Parse与Facebook集成在一起。为此,我需要在任何我想要的地方调用PFFVideosUtils logInInBackgroundWithReadPermissions方法。在整合它们的解析指南中,它告诉我们调用这个方法,并且Parse和Facebook账号将被链接,仅此而已。我调用它时,UIButton实例被触及里面,一切工作正常,但我想知道如何添加FBSDKLoginButton(简单的行为按钮,没有链接用户,只是验证他们到Facebook)默认图像到我的自定义按钮。如何将FBSDKLoginButton默认图像添加到自定义UIButton?

+0

您只想使用Facebook登录您的应用(使用Parse)。那么您打算如何使用解析进行登录? – lamdadj22

回答

0

如果我理解正确,您正在创建自己的版本FBSDKLoginButton,它可以与解析PFFacebookUtils一起使用。

最近我有同样的必要性和我用下面的方法:

  1. 创建一个UIButton - customUIButton
  2. 创建FBSDKLoginButton - fbSDKLoginButton
  3. fbSDKLoginButton.userInteractionEnabled = false
  4. 添加fbSDKLoginButton作为customUIButton子视图
  5. 将您需要的操作添加到customUIButton以处理e通过Parse自己登录 - FBSDKLoginButton对用户接触将是“透明的”,但在接收到来自SDK的登录/注销通知时会自动更新。

这里是我的示例代码中斯威夫特:

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 
     } 
    } 
} 
2

我发现了一个问题: 如何登录到解析(与创建PFUser使用所有舒适的解析的东西),使用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() 
} 
相关问题