【问题标题】:Firebase UI Auth Provider iOS Swift ExampleFirebase UI 身份验证提供程序 iOS Swift 示例
【发布时间】:2017-07-21 13:52:58
【问题描述】:

我正在启动一个新的 iOS Swift 应用程序并希望使用 FirebaseUI Auth。这是在Drop-in authentication solutionFirebase Auth 下讨论它的文档的链接。 Android 的 FirebaseUI Auth 非常简单易行。 iOS 示例似乎已经过时,因为 API 似乎在版本之间发生了巨大变化。看起来他们的版本是3.1

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

有人可以帮我提供一个示例 AppDelegate 和 ViewController 用于 facebook 和 google 登录吗?

我正在使用 Xcode 8.3、Swift 3。

Pod 文件:

# 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

这是我的 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.
}

}

这是我的视图控制器

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

    }
}
}

【问题讨论】:

    标签: ios firebase firebase-authentication firebaseui podfile


    【解决方案1】:

    您的代码看起来不错!要与 Google/Facebook/Twitter Auth 通信,您必须为您的应用添加对 URL Scheme 的支持。 CHECK THIS OUT!

    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)
    }
    

    【讨论】:

      【解决方案2】:

      你非常接近! Wei Jay 说得对,您需要在应用程序的 Info.plist 文件中定义 URL 方案,并在应用程序委托中添加回调。

      基本上你需要将以下内容添加到 plist 的根目录中。

      <key>CFBundleURLTypes</key>
      <array>
          <dict>
              <key>CFBundleURLSchemes</key>
              <array>
                  <string>com.googleusercontent.apps.{app-id-here}</string>
              </array>
          </dict>
          <dict>
              <key>CFBundleURLSchemes</key>
              <array>
                  <string>fb{app-id-here}</string>
              </array>
          </dict>
      </array>
      <key>FacebookAppID</key>
      <string>{app-id-here}</string>
      <key>FacebookDisplayName</key>
      <string>{name-here}</string>
      <key>LSApplicationQueriesSchemes</key>
      <array>
          <string>fbauth2</string>
      </array>
      

      您可以从您的 GoogleService-Info.plist 文件中的 RESERVED_CLIENT_ID 条目中获取您的 google 应用 ID。

      接下来,我的 AppDelegate 文件只是实现了 openURL 委托方法:

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

      解释here如何设置Facebook的设置

      here 这个家伙提供了一个很好的例子来说明他的 Firebase Auth UI 实现

      【讨论】:

        猜你喜欢
        • 2018-05-21
        • 1970-01-01
        • 2018-10-31
        • 2020-08-21
        • 2023-03-15
        • 2017-12-25
        • 1970-01-01
        • 2016-12-19
        • 2020-04-18
        相关资源
        最近更新 更多