gpt4 book ai didi

ios - 在 iOS (Swift) 上安装 FirebaseUI 的代码是什么

转载 作者:搜寻专家 更新时间:2023-11-01 06:05:14 24 4
gpt4 key购买 nike

我正在尝试安装 "drop-in" authentication solution来自 Firebase自动发送您的应用程序用户通过 Google、Facebook 或电子邮件/密码屏幕以对他们进行身份验证。

我的问题是 Firebase 提供的代码似乎是使用 Objective C 而不是 Swift 的旧 iOS 语言,而且一些 Swift 指导代码行似乎已经过时,而且我找不到新的行'应该使用。

我主要根据 Github 插入式解决方案这一部分的说明来构建我的代码:FirebaseUI Authentication .

这导致了以下代码,其中一些我不得不猜测,因为 Xcode 无法识别 .authUI() in let authUI = FIRAuthUI.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 类的“didFinishLaunchingWithOptions”函数中,并且我还分离了 FIRApp.configure() out 作为 AppDelegate 类中的重写,如下所示。似乎没有任何帮助。我花了大约 4 天时间试图弄清楚这一点。为 Web 和 Android 实现它只花了几个小时。

override init() {
super.init()
FIRApp.configure()
//FIRDatabase.database().persistenceEnabled = true
}

最佳答案

我终于知道怎么做了!我创建了一个 videoGIST展示如何去做,但我也会尝试在这里展示它。

首先,我将 Xcode 更新到版本 8,方法是单击“Apple App Store”,找到 Xcode,然后单击“更新”。下载需要一些时间。

其次,我通过在“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>

应该是这样的。如果您有任何问题,请告诉我!

Image of FirebaseUI sign-in screen

关于ios - 在 iOS (Swift) 上安装 FirebaseUI 的代码是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39417990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com