What Is the Code to Install FirebaseUI on iOS (Swift)

核能气质少年 提交于 2019-12-03 22:45:00

I finally figured out how to do this! I created a video and GIST to show how to do it, but I'll try to present it here as well.

First, I updated Xcode to version 8 by clicking "Apple App Store," finding Xcode, and clicking "Update." This took awhile to download.

Second, I updated AppDelegate.swift by adding FIRApp.configure() in the "didFinishLaunchingWithOptions" function.

Third, I added the following code to my AppDelegate.swift file:

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

Fourth, I updated my ViewController.swift file to this (make sure to put your own Facebook secret):

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


}

Fifth, I added a couple chunks of code to my Info.plist (you need to customize the Facebook and Google codes - see Jacob Sikorski's guide for more info on this.

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

That should be it. Let me know if you have any questions!

You're supposed to present the auth view controller on top of your view controller.

self.presentViewController(authUI?.authViewController(), animated: true, completion: nil)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!