我正在尝试安装来自"drop-in" authentication solution的Firebase,它会自动通过Google,Facebook或电子邮件/密码屏幕向您的应用用户发送身份验证。
我的问题是,Firebase提供的代码似乎是使用Objective C的旧iOS语言而不是Swift,并且某些教学Swift代码行显得过时,我找不到新行I我应该使用。
我正在构建我的代码主要来自Github插件解决方案的这部分说明:FirebaseUI Authentication。
这导致了以下代码,其中一些我不得不猜测,因为Xcode无法识别.authUI()
中的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()
分隔为AppDelegate类中的覆盖,如下所示。似乎没有任何帮助。我花了大约4天试图解决这个问题。为Web和Android实现它只需要几个小时。
override init() {
super.init()
FIRApp.configure()
//FIRDatabase.database().persistenceEnabled = true
}
答案 0 :(得分:4)
我终于想出了如何做到这一点!我创建了一个video和GIST来展示如何操作,但我也尝试在此处展示它。
首先,我通过点击" Apple App Store更新了Xcode到版本8,"找到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>
那应该是它。如果您有任何问题,请告诉我们!
答案 1 :(得分:1)
您应该在视图控制器顶部显示身份验证视图控制器。
self.presentViewController(authUI?.authViewController(), animated: true, completion: nil)