[Swift] How to implement the Twitter login function using Firebase UI ①

Introduction

Since I implemented the Twitter login function using Firebase UI when creating a personal application, We will share the command after the memorandum.

Also, I divided it into three articles because it was too long to write everything in one article. This article is the complete version and the code.

The article about the implementation procedure is below. ・ [Swift] How to implement Twitter login function using Firebase UI ②[Swift] How to implement Twitter login function using Firebase UI ③

environment ・ Swift version 5.3 ・ XCode version 12.3 ・ CocoaPods version 1.10.1

As a premise

-CocoaPods must be installed -Registered in Twitter API -Registered in Firebase console

Completed form

ezgif.com-gif-maker (2).gif

After login Firebase Authentication

You can see that the user has been added. スクリーンショット 2021-01-13 18.48.11.png

code

AppDelegate.swift



import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

}

ViewController.swift



import UIKit
import Firebase
import FirebaseUI

class ViewController: UIViewController, FUIAuthDelegate {

    let authUI = FUIAuth.defaultAuthUI()
    let providers: [FUIAuthProvider] = [
        FUIOAuth.twitterAuthProvider()
    ]
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        authUI!.delegate = self
        authUI!.providers = providers
        
        checkLoggedIn()
    }
    
    //Login(Login failure)Method called later
    func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?) {
        // handle user and error as necessary
    }

    func checkLoggedIn() {

        Auth.auth().addStateDidChangeListener{auth, user in
            if user != nil{
                print("success")
            } else {
                print("fail")
                self.login()
            }
        }
    }
    
    func login() {
        let authViewController = authUI!.authViewController()
        self.present(authViewController, animated: true, completion: nil)
    }
}

Recommended Posts

[Swift] How to implement the Twitter login function using Firebase UI ①
[Swift] How to implement the Twitter login function using Firebase UI ②
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
[Swift] How to implement the countdown function
How to implement the breadcrumb function using gretel
[Swift] How to implement the fade-in / out function
[Swift5] How to implement animation using "lottie-ios"
[Swift] How to link the app with Firebase
[For beginners] How to implement the delete function
Rails learning How to implement search function using ActiveModel
[Swift] How to get the document ID of Firebase
[Swift] I tried to implement the function of the vending machine
[Swift 5] Implement UI for review function
How to add the delete function
[Swift UI] How to get the startup status of the application [iOS]
[Behavior confirmed in December 2020] How to implement the alert display function
How to implement UI automated test using image comparison in Selenium
Try to implement login function with Spring-Boot
How to implement TextInputLayout with validation function
[Swift5] How to implement standby screen using'PKHUD'
[Processing × Java] How to use the function
How to implement image posting using rails
A story about using the CoreImage framework to erase stains with Swift and implement a blur erase function
How to overwrite Firebase data in Swift
How to return a value from Model to Controller using the [Swift5] protocol
[Swift] How to set an image in the background without using UIImageView.
How to implement the email authentication function at the time of user registration
Implement the star five function using the for statement
How to add sound in the app (swift)
Implement the product category function using ancestry ① (Preparation)
Try to implement login function with Spring Boot
Flow to implement image posting function using ActiveStorage
[Swift UI] How to disable ScrollsToTop of ScrollView
How to get the ID of a user authenticated with Firebase in Swift
How to implement image posting function using Active Storage in Ruby on Rails
Continued ・ Flow to implement image posting function using ActiveStorage
How to implement login request processing (Rails / for beginners)
How to implement guest login in 5 minutes in rails portfolio
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
Create a login authentication screen using the session function
Try to implement tagging function using rails and js
I tried to implement the like function by asynchronous communication
How to transition from the [Swift5] app to the iPhone settings screen
How to easily implement in-app purchase using itemstore <Implementation: Android>
How to get the longest information from Twitter as of 12/12/2016
How to implement infinite scrolling (page nate) in Swift TableView
How to install JDK 8 on Windows without using the installer
[Swift] How to install Firebase ~ How to use Realtime Database & Cloud Firestore
[Rails] I tried to implement "Like function" using rails and js
How to change BackgroundColor etc. of NavigationBar in Swift UI
How to play MIDI files using the Java Sound API
How to add ActionText function
[Swift] How to use UserDefaults
How to use Swift UIScrollView
Implement category function using ancestory
[Rails] How to implement scraping
[Java] How to implement multithreading
[Swift] Implement UITableView using xib
How to authorize using graphql-ruby
[Rails] How to convert the URI of the image sent by http to https when using Twitter API