我在UICollectionViewCell中使用FBSDK时遇到问题。我收到警告,在返回语句后代码不会执行。我已成功在UIViewController中实现了Facebook登录,但在尝试在CollectionViewCell类中执行时遇到错误。感谢您的帮助!
class LoginCell: UICollectionViewCell, FBSDKLoginButtonDelegate {
lazy var FBloginButton: UIButton = {
let customButton = UIButton(type: .system)
customButton.backgroundColor = UIColor.blue
customButton.setTitle("Login With Facebook", for: .normal)
customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
customButton.setTitleColor(.white, for: .normal)
return customButton
customButton.addTarget(self, action: #selector(handleCustomFBButton), for: .touchUpInside)
}()
// am I passing an objective from: correctly below...????
func handleCustomFBButton() {
FBSDKLoginManager().logIn(withReadPermissions: ["email", "public_profile"], from: self.delegate as! UIViewController!) { (result, err) in
if err != nil {
print("Custom FB Login Button Failed")
return
}
self.showEmailAddress()
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("did log out of facebook...")
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if error != nil {
print(error)
return
}
showEmailAddress()
}
func showEmailAddress() {
let accessToken = FBSDKAccessToken.current()
guard let accessTokenString = accessToken?.tokenString else { return }
let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)
FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
if error != nil {
print("Something went wrong with our FB user: ", error ?? "")
return
}
print("Successfully logged in with our user: ", user ?? "")
})
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start { (connection, result, err) in
if err != nil {
print("Failed to start graph request:", err ?? "")
return
}
print(result ?? "")
}
}
weak var delegate: LoginControllerDelegate?
func handleLogin() {
delegate?.finishLoggingIn()
}
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(logoImageView)
addSubview(emailTextField)
addSubview(passwordTextField)
addSubview(loginButton)
addSubview(FBloginButton)
_ = logoImageView.anchor(centerYAnchor, left: nil, bottom: nil, right: nil, topConstant: -230, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 160, heightConstant: 160)
logoImageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
_ = FBloginButton.anchor(loginButton.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 32, bottomConstant: 0, rightConstant: 32, widthConstant: 0, heightConstant: 50)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
答案 0 :(得分:0)
您在设定目标前已返回。
尝试在customButton.addTarget(self, action: #selector(handleCustomFBButton), for: .touchUpInside)
语句之前放置return customButton
语句。
答案 1 :(得分:0)
return
时
该功能实施。return
语句都不会执行。lazy var FBloginButton: UIButton
,发布return
语句按钮 - 正在设置目标。解决问题的适当实施方式如下:
lazy var FBloginButton: UIButton = {
let customButton = UIButton(type: .system)
customButton.backgroundColor = UIColor.blue
customButton.setTitle("Login With Facebook", for: .normal)
customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
customButton.setTitleColor(.white, for: .normal)
customButton.addTarget(self, action: #selector(handleCustomFBButton), for: .touchUpInside)
return customButton
}()