ios - 在ios中使用swift 3进行Google和Facebook Firebase登录身份验证

标签 ios swift firebase-authentication facebook-login google-signin

我想在使用 firebase auth 的 View 中实现使用 google 登录和使用 Facebook 登录

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let handled =  FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication,annotation: annotation)
        return handled
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url,
                                                 sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,annotation: [:])
    }
}

ViewController.swift

class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self 
    }

    @IBAction func GSignInPressed(_ sender: Any) {
        print("GsignIn pressed")
        GIDSignIn.sharedInstance().signIn()
    }

    @IBAction func fbPressed(_ sender: Any) {

        let facebookLogin = FBSDKLoginManager()

        facebookLogin.logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, error) in
            if error != nil {
                print("uhhhh! unable to connect with facebook")
            } else if result?.isCancelled == true {
                print("Uhhh! User cancelled FB auth")
            } else {
                print("Uhhh! Sucessfully authenticated with FB")
                let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
                self.firebaseAuth(credential)
            }
        }
    }

    func firebaseAuth(_ credential: AuthCredential) {

        Auth.auth().signIn(with: credential) { (user, error) in
            if error != nil {
                // ...
                print("Error in credentials")
                print(error!)
                return
            }
            // User is signed in
            // ...
            let email = user?.email
            print(email!)
        }
    }

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if error != nil {
            print("error in google sign in")
            return
        }
        guard let authentication = user.authentication else { return }
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,accessToken: authentication.accessToken)
        self.firebaseAuth(credential)
    }
}

最佳答案

 //MARK: - Handle URL for FB and Google Sign -
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

    //handle the URL that your application receives at the end of the authentication process -
    var flag: Bool = false
    // handle Facebook url scheme
    if let wasHandled:Bool = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {
        flag = wasHandled
    }
    // handle Google url scheme
    if let googlePlusFlag: Bool = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication!, annotation: annotation) {
        flag = googlePlusFlag
    }
    return flag
}

在 AppDelegate 中尝试使用此方法

关于ios - 在ios中使用swift 3进行Google和Facebook Firebase登录身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45755126/

相关文章:

ios - 如何使用 firebase 在 xcode 中验证电子邮件和密码

swift - 使用 Facebook 凭据删除帐户

ios - 取消完成处理程序 (iOS)

ios - 线程 1 : SIGABRT error in simple storyboard app

ios - 快速将 if else 结构转换为 switch case

arrays - 带有枚举的 Swift 类数组

ios - iTunesConnect 始终将应用程序保存在 "pre release"中?

ios - Storyboard与模拟器不同

ios - 我怎样才能调用其他类按钮 - Swift

firebase - 如何使用 Ionic 4 实现 Firebase 电话身份验证?