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

This article is This is a continuation of [Swift] How to implement the Twitter login function using Firebase UI ①. In (1), the complete form and the entire code are described.

See this article for implementation instructions.

Implementation method

I will explain the implementation method from the beginning. Please note that the screen may differ slightly depending on the version.

Also, please refer to the separate article for the cooperation between Firebase and the app. -> Firebase and App Linkage Procedure

This article is based on the assumption that the article has been completed.

Firebase project creation

Open the Firebase console. Select Build> Authentication and click Start. スクリーンショット 2021-01-13 7.17.21.png Click to display the following screen. スクリーンショット 2021-01-13 7.17.33.png When you click on Twitter, the following screen will appear, so Check Enable to enable Twitter login. (You will enter the API key and API secret later.) スクリーンショット 2021-01-13 7.20.03.png

Creating apps with Twitter Developers

Log in to Twitter Developer and click Overview to scroll down. Then I think there is a + Create App button, so click on it. スクリーンショット 2021-01-13 6.49.43.png Specify an appropriate name and create it. (If the name is covered, it cannot be created.) スクリーンショット 2021-01-13 7.27.38.png

Cooperation between Firebase and Twitter Developers

If you can create it, you will move to the next screen. Copy and paste the API key and API secret key here into Firebase. スクリーンショット 2021-01-13 7.28.45.png Please copy and paste each applicable part. スクリーンショット 2021-01-13 7.30.28.png Go back to Twitter Developers and click App settings. スクリーンショット 2021-01-13 7.31.28.png Click Edit under Authentication setting. スクリーンショット 2021-01-13 7.32.53.png Enable Enable 3-legged OAuth. スクリーンショット 2021-01-13 7.42.46.png Enter the Callback URLs and the Website URL.

In Callback URLs, under the place where you entered the API key and API secret key of Firebase earlier The URL starting with https: // is listed, so copy and paste it.

For Website URL, enter the URL of your website. I entered the URL of the Hatena blog, but there is no such thing! For those who say Please create it on the site called Peraichi and paste the URL there. スクリーンショット 2021-01-13 7.44.31.png After entering, click save. It's OK if it says 3-legged OAuth is enabled! スクリーンショット 2021-01-13 7.49.47.png

Implementation of login function within the app

Next, implement the login function.

To implement the login function Import Firebase UI and Firebase.

ViewController.swift



import Firebase
import FirebaseUI

Then instantiate the default UI into the authUI. Also, configure FirebaseUI to use the supported login methods.

Also, checkLoggedIn () is called when the screen is loaded. checkLoggedIn () will be explained later.

ViewController.swift



let authUI = FUIAuth.defaultAuthUI()
let providers: [FUIAuthProvider] = [
    FUIOAuth.twitterAuthProvider()
]

override func viewDidLoad() {
    super.viewDidLoad()
        
    authUI!.delegate = self
    authUI!.providers = providers
        
    checkLoggedIn()    
}

I get an error with authUI! .Delegate = self, so fix it. (An error will occur because it does not comply with the FUIAuthDelegate protocol.)

The protocol should be added as shown below.

ViewController.swift



class ViewController: UIViewController, FUIAuthDelegate { }

CheckLoggedIn () called by viewDidLoad, Check if the user has been added.

With Auth.auth (). addStateDidChangeListener {} If a user exists, the information is stored in user.

If it doesn't exist, nil is returned, so self.login () is executed.

ViewController.swift


func checkLoggedIn() {

    Auth.auth().addStateDidChangeListener{auth, user in
        if user != nil{
            print("success")
        } else {
            print("fail")
            self.login()
        }
    }
}

login () processes the screen transition.

With let authViewController = authUI! .AuthViewController () View information is assigned to the constant authViewController.

The screen transition is performed by present () after that.

ViewController.swift



func login() {
    let authViewController = authUI!.authViewController()
    self.present(authViewController, animated: true, completion: nil)
}

at the end

For the time being, the login function can be implemented.

However, the login screen matches in English, or it is not full screen, I thought it would be a little inconvenient if I didn't customize it.

For the time being, with the cooperation of Firebase and the application Implementation method You should be able to implement Twitter login by following steps ① and ② in order!

Since the screen status and code may differ depending on the version, In that case, please investigate and implement it yourself!

It's been a long time, but thank you for watching until the end.

reference

· Easy Add Login to iOS App with Firebase UI -Implement Twitter loginProcedure for linking Firebase and apps

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
[Swift] How to get the document ID of Firebase
[Swift 5] Implement UI for review function
How to add the delete function
[Behavior confirmed in December 2020] How to implement the alert display function
How to implement UI automated test using image comparison in Selenium
[Java] How to use the hasNext function
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
Continued ・ Flow to implement image posting function using ActiveStorage
How to implement UICollectionView in Swift with code only
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 get the longest information from Twitter as of 12/12/2016
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
[Swift] Implement UITableView using xib
How to authorize using graphql-ruby
How to create a jar file or war file using the jar command
How to create a registration / update function where the table crosses
[Swift] How to display when the quiz app answers correctly [Beginner]
Try using the two-point measurement function of Firebase Performance Monitoring. [Android]
How to get the contents of Map using for statement Memorandum
[Rails] How to put a crown mark on the ranking function
How to solve the unknown error when using slf4j in Java