在ios和swift中使用firebase登录Facebook,但无法获取用户凭据

在ios和swift中使用firebase登录Facebook,但无法获取用户凭据,ios,swift,facebook-login,firebase-authentication,Ios,Swift,Facebook Login,Firebase Authentication,我正在尝试将facebook登录集成到我的应用程序中(已经设置了GOOGLE登录),但在调用凭据时,didCompleteWithResult功能中 let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString) 我得到一个错误: 致命错误:在展开可选值时意外发现nil 每次按下登录按钮,我都必须输入我的电子邮件地址和密码

我正在尝试将facebook登录集成到我的应用程序中(已经设置了GOOGLE登录),但在调用凭据时,
didCompleteWithResult
功能中

let credential =  FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
我得到一个错误:
致命错误:在展开可选值时意外发现nil

每次按下登录按钮,我都必须输入我的电子邮件地址和密码,在此之前,我会遇到另一个错误:
2016-07-04 00:34:54.228预路径签名[9693:438627]-canOpenURL:URL失败:“fbauth2:/”-错误:“(空)”
2016-07-04 00:34:54.248预路径签名[9693:438627]-canOpenURL:URL失败:“fbauth2:/”-错误:“(空)”
在我登录safari页面后,不会将我带回我的应用程序页面

我的appdelegate文件:

import UIKit
import CoreData
import Firebase
import IQKeyboardManagerSwift
import GoogleSignIn
import FBSDKCoreKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate , GIDSignInDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    FIRApp.configure()

    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

    IQKeyboardManager.sharedManager().enable = true

    GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
    GIDSignIn.sharedInstance().delegate = self

    return true
}



////////////////////////////////////////
//
//Google signin
//

func application(application: UIApplication,
    openURL url: NSURL, options: [String: AnyObject]) -> Bool {
        GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
            annotation: options[UIApplicationOpenURLOptionsAnnotationKey])


        return true
}


func application(application: UIApplication,
    openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        var options: [String: AnyObject] = [UIApplicationOpenURLOptionsSourceApplicationKey: sourceApplication!,
            UIApplicationOpenURLOptionsAnnotationKey: annotation]
       GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: sourceApplication,
            annotation: annotation)

        let handled = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)


         return  handled
}

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
    withError error: NSError!) {
        if let error = error {
            print(error.localizedDescription)
            return
        }
        // ...


func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
    withError error: NSError!) {
        // Perform any operations when the user disconnects from app here.
        // ...
        }



}
                                        //
         }
和我的LoginView控制器:

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

    print("RESULT : \(result)")
    print("ERROR : \(error)")

    let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)

    FIRAuth.auth()?.signInWithCredential(credential, completion: {
    (user, error) in

        if error != nil{

            self.signInButton.enabled = true
            print("Problem at signing in with facebook with error : \(error)")
            self.showAlert("Error Signing in With Facebook", message: "Please enter the correct email and password")


        }else {
        print("USER LOGGED IN THROUGH FACEBOOK")
            _ = self.ref.child("users").observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) -> Void in
                let usersDict = snapshot.value as? [String : AnyObject]

                print(usersDict)
                if let userDetails = usersDict![user!.uid] as? [String : AnyObject], let _ = userDetails["username"] as? String
                {

                    let homePageScene = self.navigationController?.storyboard?.instantiateViewControllerWithIdentifier("HomePageViewControllerVC_ID") as! HomePageViewController
                    self.navigationController?.pushViewController(homePageScene, animated: true)

                }
                else{

                    let userNamePageScene = self.navigationController?.storyboard?.instantiateViewControllerWithIdentifier("UsernameViewControllerVC_ID") as! UsernameViewController

                    self.navigationController?.pushViewController(userNamePageScene, animated: true)
                }
            })
        }
    })
}

我是一个新手…感谢任何帮助

要检索providerData,您现在需要这样的内容

if let user = user {
} else {
print("User cancelled the Facebook login"))
}
您需要检查是否有用户,或者该用户是否已取消登录。像这样:

if let user = FIRAuth.auth()?.currentUser {
for profile in user.providerData {
let providerId = profile.providerId
let uid = profile.uid;  // Provider-specific UID
let name = profile.displayName
let email = profile.email
let photoUrl = profile.photoURL
}
} else {
// No user logged in.
}



func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError?) {
if let error = error {
    print(error.localizedDescription)
    return
}
self.performSegueWithIdentifier("login", sender: self)
}
或其他一些补充:

let currentToken = FBSDKAccessToken.currentAccessToken()

// Show login if this is first time logging in
if (currentToken == nil || !currentToken.hasGranted("email")) {

    let ref = Firebase(url: "https://my-app.firebaseio.com")
    let facebookLogin = FBSDKLoginManager()
    facebookLogin.logInWithReadPermissions(["email"], fromViewController: self, handler: {
        (facebookResult, facebookError) -> Void in
        if facebookError != nil {
            log.error("Facebook login failed. Error \(facebookError)")

谢谢你的回复。我这样做了,但它仍然给我一个错误。你能解释一下什么是
2016-07-04 00:34:54.228 PrePathSigningIn[9693:438627]-canOpenURL:URL失败:“fbauth2:/”-错误:“(null)”
的意思吗?这是一个Xcode警告,指示
canOpenURL:
调用返回false。只要在plist中配置了LSApplicationQueriesSchemes条目,就可以忽略此警告。这意味着FB应用程序未安装在您正在运行的设备/模拟器上。