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
-CocoaPods must be installed -Registered in Twitter API -Registered in Firebase console
You can see that the user has been added.
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