在 iOS (Swift) 上安装 FirebaseUI 的代码是什么
What Is the Code to Install FirebaseUI on iOS (Swift)
我正在尝试安装 "drop-in" authentication solution from Firebase,它会自动让您的应用用户通过 Google、Facebook 或 email/password 屏幕进行身份验证。
我的问题是 Firebase 提供的代码似乎使用的是 Objective C 的旧 iOS 语言,而不是 Swift,并且一些指导行 Swift 代码似乎已过时,我找不到我应该使用的新行。
我主要根据 Github 插入式解决方案这一部分的说明构建我的代码:FirebaseUI Authentication.
这导致了以下代码,其中一些我不得不猜测,因为 Xcode 无法识别 let authUI = FIRAuthUI.authUI()
中的 .authUI()
。该应用程序不会崩溃,但它也根本不会启动身份验证过程。这只是一个空白屏幕。
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabaseUI
import FirebaseAuthUI
import FirebaseGoogleAuthUI
import FirebaseFacebookAuthUI
import FBSDKCoreKit
import FBSDKLoginKit
class LoginController: UIViewController, FIRAuthUIDelegate {
var db = FIRDatabaseReference.init()
var kFacebookAppID = "15839856152xxxxx"
var kGoogleClientID = "9042861xxxxx-6qq4gmeos07gpgmgt54ospv3fvpg0724.apps.googleusercontent.com"
override func viewDidLoad() {
super.viewDidLoad()
//FIRApp.configure()
let authUI = FIRAuthUI.defaultAuthUI()
let facebookAuthUI = FIRFacebookAuthUI(appID: kFacebookAppID)
let googleAuthUI = FIRGoogleAuthUI(clientID: kGoogleClientID)
//let emailAuthUI = FIREmailPasswordAuthProvider
authUI?.providers = [facebookAuthUI, googleAuthUI]
// Present the auth view controller and then implement the sign in callback.
let authViewController = authUI
authUI?.authViewController()
}
func authUI(authUI: FIRAuthUI, didSignInWithUser user: FIRUser?, error: NSError?) {
if error != nil {
//Problem signing in
}else {
//User is in!
}
}
func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
let sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String
return FIRAuthUI.defaultAuthUI()!.handleOpenURL(url, sourceApplication: sourceApplication ?? "") ?? false
}
}
另一方面,我将 FIRApp.configure()
放在 AppDelegate class 的 "didFinishLaunchingWithOptions" 函数中,并且我还在AppDelegate class,如下图。似乎没有任何帮助。我花了大约 4 天时间试图弄清楚这一点。为 Web 和 Android 实施它只花了几个小时。
override init() {
super.init()
FIRApp.configure()
//FIRDatabase.database().persistenceEnabled = true
}
您应该在您的视图控制器之上显示 auth 视图控制器。
self.presentViewController(authUI?.authViewController(), animated: true, completion: nil)
我终于知道怎么做了!我创建了一个 video and GIST 来展示如何做,但我也会尝试在这里展示它。
首先,我通过单击 "Apple App Store," 找到 Xcode,然后单击 "Update." 将 Xcode 更新到版本 8。这需要一段时间才能下载。
其次,我通过在 "didFinishLaunchingWithOptions" 函数中添加 FIRApp.configure()
来更新 AppDelegate.swift。
第三,我将以下代码添加到我的 AppDelegate.swift 文件中:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
return handled || GIDSignIn.sharedInstance().handle(
url,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
第四,我将我的 ViewController.swift 文件更新为这个(确保输入你自己的 Facebook 秘密):
// ViewController.swift
// Bizzy Books
//
// Created by Brad Caldwell on 9/23/16.
// Copyright © 2016 Caldwell Contracting LLC. All rights reserved.
//
import UIKit
import Firebase
import FirebaseAuthUI
import FirebaseDatabaseUI
import FirebaseGoogleAuthUI
import FirebaseFacebookAuthUI
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FIRAuthUIDelegate {
//var db = FIRDatabaseReference.init()
var kFacebookAppID = "PLACE YOUR 16-DIGIT FACEBOOK SECRET HERE (FOUND IN FIREBASE CONSOLE UNDER AUTHENTICATION)"
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 = FIRAuthUI.init(auth: FIRAuth.auth()!)
let options = FIRApp.defaultApp()?.options
let clientId = options?.clientID
let googleProvider = FIRGoogleAuthUI(clientID: clientId!)
let facebookProvider = FIRFacebookAuthUI(appID: kFacebookAppID)
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: FIRAuthUI, didSignInWith user: FIRUser?, error: Error?) {
if error != nil {
//Problem signing in
login()
}else {
//User is in! Here is where we code after signing in
}
}
func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
let sourceApplication = options[UIApplicationOpenURLOptionUniversalLinksOnly] as! String
return FIRAuthUI.default()!.handleOpen(url as URL, sourceApplication: sourceApplication )
}
}
第五,我在我的 Info.plist 中添加了几个代码块(您需要自定义 Facebook 和 Google 代码 - 有关详细信息,请参阅 Jacob Sikorski's guide。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.PLACE YOUR LONG CODE HERE ***(mine is 12 digits followed by a dash followed by 32 alpha-numeric characters)***</string>
<string>PLACE YOUR FACEBOOK CODE HERE ***(mine said fb followed by 16 numbers)***</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fbauth2</string>
<string>fb-messenger-api20140430</string>
</array>
应该是这样。如果您有任何问题,请告诉我!
我正在尝试安装 "drop-in" authentication solution from Firebase,它会自动让您的应用用户通过 Google、Facebook 或 email/password 屏幕进行身份验证。
我的问题是 Firebase 提供的代码似乎使用的是 Objective C 的旧 iOS 语言,而不是 Swift,并且一些指导行 Swift 代码似乎已过时,我找不到我应该使用的新行。
我主要根据 Github 插入式解决方案这一部分的说明构建我的代码:FirebaseUI Authentication.
这导致了以下代码,其中一些我不得不猜测,因为 Xcode 无法识别 let authUI = FIRAuthUI.authUI()
中的 .authUI()
。该应用程序不会崩溃,但它也根本不会启动身份验证过程。这只是一个空白屏幕。
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabaseUI
import FirebaseAuthUI
import FirebaseGoogleAuthUI
import FirebaseFacebookAuthUI
import FBSDKCoreKit
import FBSDKLoginKit
class LoginController: UIViewController, FIRAuthUIDelegate {
var db = FIRDatabaseReference.init()
var kFacebookAppID = "15839856152xxxxx"
var kGoogleClientID = "9042861xxxxx-6qq4gmeos07gpgmgt54ospv3fvpg0724.apps.googleusercontent.com"
override func viewDidLoad() {
super.viewDidLoad()
//FIRApp.configure()
let authUI = FIRAuthUI.defaultAuthUI()
let facebookAuthUI = FIRFacebookAuthUI(appID: kFacebookAppID)
let googleAuthUI = FIRGoogleAuthUI(clientID: kGoogleClientID)
//let emailAuthUI = FIREmailPasswordAuthProvider
authUI?.providers = [facebookAuthUI, googleAuthUI]
// Present the auth view controller and then implement the sign in callback.
let authViewController = authUI
authUI?.authViewController()
}
func authUI(authUI: FIRAuthUI, didSignInWithUser user: FIRUser?, error: NSError?) {
if error != nil {
//Problem signing in
}else {
//User is in!
}
}
func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
let sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String
return FIRAuthUI.defaultAuthUI()!.handleOpenURL(url, sourceApplication: sourceApplication ?? "") ?? false
}
}
另一方面,我将 FIRApp.configure()
放在 AppDelegate class 的 "didFinishLaunchingWithOptions" 函数中,并且我还在AppDelegate class,如下图。似乎没有任何帮助。我花了大约 4 天时间试图弄清楚这一点。为 Web 和 Android 实施它只花了几个小时。
override init() {
super.init()
FIRApp.configure()
//FIRDatabase.database().persistenceEnabled = true
}
您应该在您的视图控制器之上显示 auth 视图控制器。
self.presentViewController(authUI?.authViewController(), animated: true, completion: nil)
我终于知道怎么做了!我创建了一个 video and GIST 来展示如何做,但我也会尝试在这里展示它。
首先,我通过单击 "Apple App Store," 找到 Xcode,然后单击 "Update." 将 Xcode 更新到版本 8。这需要一段时间才能下载。
其次,我通过在 "didFinishLaunchingWithOptions" 函数中添加 FIRApp.configure()
来更新 AppDelegate.swift。
第三,我将以下代码添加到我的 AppDelegate.swift 文件中:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
return handled || GIDSignIn.sharedInstance().handle(
url,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
第四,我将我的 ViewController.swift 文件更新为这个(确保输入你自己的 Facebook 秘密):
// ViewController.swift
// Bizzy Books
//
// Created by Brad Caldwell on 9/23/16.
// Copyright © 2016 Caldwell Contracting LLC. All rights reserved.
//
import UIKit
import Firebase
import FirebaseAuthUI
import FirebaseDatabaseUI
import FirebaseGoogleAuthUI
import FirebaseFacebookAuthUI
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FIRAuthUIDelegate {
//var db = FIRDatabaseReference.init()
var kFacebookAppID = "PLACE YOUR 16-DIGIT FACEBOOK SECRET HERE (FOUND IN FIREBASE CONSOLE UNDER AUTHENTICATION)"
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 = FIRAuthUI.init(auth: FIRAuth.auth()!)
let options = FIRApp.defaultApp()?.options
let clientId = options?.clientID
let googleProvider = FIRGoogleAuthUI(clientID: clientId!)
let facebookProvider = FIRFacebookAuthUI(appID: kFacebookAppID)
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: FIRAuthUI, didSignInWith user: FIRUser?, error: Error?) {
if error != nil {
//Problem signing in
login()
}else {
//User is in! Here is where we code after signing in
}
}
func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
let sourceApplication = options[UIApplicationOpenURLOptionUniversalLinksOnly] as! String
return FIRAuthUI.default()!.handleOpen(url as URL, sourceApplication: sourceApplication )
}
}
第五,我在我的 Info.plist 中添加了几个代码块(您需要自定义 Facebook 和 Google 代码 - 有关详细信息,请参阅 Jacob Sikorski's guide。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.PLACE YOUR LONG CODE HERE ***(mine is 12 digits followed by a dash followed by 32 alpha-numeric characters)***</string>
<string>PLACE YOUR FACEBOOK CODE HERE ***(mine said fb followed by 16 numbers)***</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fbauth2</string>
<string>fb-messenger-api20140430</string>
</array>
应该是这样。如果您有任何问题,请告诉我!