我试图通过FirebaseUI Auth连接Facebook登录但是我无法让Facebook连接按钮出现在实例化的authViewController中。平台:iOS,语言:Swift 2.0,最新稳定的Xcode版本
就拉出一个实例化的authViewController而言,一切正常。电子邮件/密码选项存在,并在我的Firebase数据库中主动创建新用户。
尝试实施Facebook登录功能时遇到问题;
在我的AppDelegate.swift文件中,我有以下内容:
import Firebase
import FirebaseAuthUI
...
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
let authUI = FIRAuthUI.authUI()!
authUI.delegate = self
return true
}
我得到的错误是"无法分配类型' AppDelegate'输入' FIRAuthUIDelegate'" - 基本上我不能让下面一行工作; authUI.delegate = self
我已阅读所有文档,并在项目的gitHub回购中浏览了有关实施的开放/已关闭问题: https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseUI
我查看了之前类似的StackOverflow问题; How to use FirebaseUI for Google authentication on iOS in Swift?
我试图从上面的Stack Overflow中复制代码,因为他说他确实使用他的代码获得了更多选项(他的问题是关于身份验证的更多错误)但我仍然遇到了同样的自我委派错误。我已经尝试将FIRAuthUIDelegate分配给AppDelegate,但它不符合。我错过了什么?
答案 0 :(得分:0)
我没有使用Firebase,但似乎您应该将代码放在视图控制器中......
import UIKit
import Firebase
import FirebaseAuthUI
class ViewController: UIViewController, FIRAuthUIDelegate {
override func viewDidLoad() {
super.viewDidLoad()
FIRApp.configure()
let authUI = FIRAuthUI.authUI()!
authUI.delegate = self
}
同样,我之前没有使用过Firebase,但这是代理人通常的工作方式。
答案 1 :(得分:0)
我也不熟悉FirebaseUI,但这是一个使用常规Firebase和FBSDK授权用户使用Facebook的工作示例
@IBAction func fbButtonTapped(sender: UIButton) {
let facebookReadPermissions = ["email", "public_profile", "user_photos"]
FBSDKLoginManager().logInWithReadPermissions(facebookReadPermissions, fromViewController: self, handler: { (result:FBSDKLoginManagerLoginResult?, error:NSError?) -> Void in
if error != nil {
Helper.displayAlert("Error Logging into Facebook", message: error!.localizedDescription, viewController: self)
} else {
let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
if error != nil {
Helper.displayAlert("Error Logging into Facebook", message: error!.localizedDescription, viewController: self)
} else {
let request = FBSDKGraphRequest(graphPath:"me", parameters: ["fields": "id, first_name, last_name, email, age_range, gender, verified, timezone, picture"])
request.startWithCompletionHandler {
(connection, result, error) in
if error != nil {
print (error)
} else if let userData = result as? [String : AnyObject] {
guard let userID = FIRAuth.auth()?.currentUser?.uid else { return }
let userInfo = ["firstName": userData["first_name"] as! String, "lastName": userData["last_name"] as! String, "email": userData["email"] as! String,
"gender": userData["gender"] as! String, "id": userData["id"] as! String, "verified": userData["verified"]?.description as! AnyObject, "key": userID]
FirebaseData.fbData.createFirebaseUser(userID, user: userInfo)
self.performSegueWithIdentifier(self.loginSucessIdentifier, sender: nil)
}
}
}
}
}
})
}
func createFirebaseUser(uid: String, user: [String : AnyObject]) {
REF_USERS.child(uid).setValue(user)
}
可以清除代码以消除所有if语句,但它应该让你使用facebook登录的工作授权。