Firebase UI Auth Provider iOS Swift示例

Sco*_*t B 6 ios firebase firebase-authentication firebaseui podfile

我正在开始一个新的iOS Swift应用程序,并希望使用FirebaseUI Auth.以下是在Drop-in authentication solution Firebase Auth下与其进行讨论的文档的链接.适用于Android的FirebaseUI Auth非常简单易用.似乎iOS示例已过时,因为API似乎在版本之间发生了巨大变化.它看起来像3.1版本.

方向也有点裸露:https://github.com/firebase/FirebaseUI-iOS

有人可以帮助我,并为Facebook和谷歌登录提供一个示例AppDelegate和ViewController?

我正在使用Xcode 8.3,Swift 3.

Podfile:

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'Project' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  pod 'FirebaseUI', '~> 3.1'
  pod 'Firebase/Core'
  pod 'Firebase/Database'
  pod 'Firebase/Crash'
  pod 'Firebase/Auth'
  pod 'Firebase/Storage'
  pod 'GoogleSignIn'
  pod 'FBSDKLoginKit'


  target 'ProjectTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'ProjectUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end
Run Code Online (Sandbox Code Playgroud)

这是我的AppDelegate

import UIKit
import CoreData
import Firebase
import FirebaseAuthUI
import FirebaseAuth
import GoogleSignIn
import FBSDKLoginKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

}
Run Code Online (Sandbox Code Playgroud)

这是我的ViewController

import UIKit
import Firebase
import FirebaseAuth
import FirebaseAuthUI
import FirebaseDatabaseUI
import FirebaseGoogleAuthUI
import FirebaseFacebookAuthUI
import FBSDKCoreKit
import FBSDKLoginKit

class ViewController: UIViewController, FUIAuthDelegate {

    var kFacebookAppID = "111111111111111"
override func viewDidLoad() {
    super.viewDidLoad()

    //FIRApp.configure()
    checkLoggedIn()
}

func checkLoggedIn() {
    FIRAuth.auth()?.addStateDidChangeListener { auth, user in
        if user != nil {
            // User is signed in.
        } else {
            // No user is signed in.
            self.login()
        }
    }
}

func login() {
    let authUI = FUIAuth.defaultAuthUI()
    let facebookProvider = FUIGoogleAuth()
    let googleProvider = FUIFacebookAuth()
    authUI?.delegate = self
    authUI?.providers = [googleProvider, facebookProvider]
    let authViewController = authUI?.authViewController()
    self.present(authViewController!, animated: true, completion: nil)
}

@IBAction func logoutUser(_ sender: AnyObject) {
    try! FIRAuth.auth()!.signOut()
}

func authUI(_ authUI: FUIAuth, didSignInWith user: FIRUser?, error: Error?) {
    if error != nil {
        //Problem signing in
        login()
    }else {
        //User is in! Here is where we code after signing in

    }
}
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*jay 1

你的代码看起来不错!要与 Google/Facebook/Twitter Auth 进行通信,您必须向应用程序添加对URL 方案的支持。看一下这个!

func configureAuth() {
    // TODO: configure firebase authentication
    let provider: [FUIAuthProvider] = [FUIGoogleAuth(), FUIFaceBookAuth()]
    FUIAuth.defaultAuthUI()?.providers = provider 

    // listen for changes in the authorization state
    _authHandle = FIRAuth.auth()?.addStateDidChangeListener { (auth: FIRAuth, user: FIRUser?) in

        // check if there is a current user
        if let activeUser = user {
            // check if current app user is the current FIRUser
            if self.user != activeUser {
                // sign in
            }
        } else {
            // user must sign in
            self.loginSession()
        }
    }


}

func loginSession() {
    let authViewController = FUIAuth.defaultAuthUI()!.authViewController()
    self.present(authViewController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)