Brad Caldwell
Brad Caldwell

Reputation: 343

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

I'm trying to install the "drop-in" authentication solution from Firebase that automatically sends your app users through Google, Facebook, or email/password screens to authenticate them.

My problem is that the code provided by Firebase appears to be in the old iOS language of Objective C instead of Swift, and that some of the lines of instructional Swift code appear outdated, and I can't find the new lines I'm supposed to use.

I'm building my code mostly from the instructions on this part of the Github drop-in solution: FirebaseUI Authentication.

This has resulted in the following code, some of which I've had to guess at, since Xcode couldn't recognize .authUI() in let authUI = FIRAuthUI.authUI(). The app doesn't crash, but it's not starting the authentication process at all, either. It's just a blank screen.

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
}

}

As a side not, I placed FIRApp.configure() inside the "didFinishLaunchingWithOptions" function in the AppDelegate class, and I have also separated FIRApp.configure() out as an override in the AppDelegate class, as shown below. Nothing seems to help. I've spent about 4 days trying to figure this out. Implementing it for Web and Android only took a few hours.

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

Upvotes: 1

Views: 2247

Answers (2)

Brad Caldwell
Brad Caldwell

Reputation: 343

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!

Image of FirebaseUI sign-in screen

Upvotes: 4

Chao Wei
Chao Wei

Reputation: 334

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

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

Upvotes: 1

Related Questions