[Swift] First Firebase

Introduction

Life is Tech! Members Advent Calendar 2020 will be in charge of the 25th day! It's exciting! (@sugijotaro) I usually make iPhone apps, touch Unity, and make videos.

In this article, we'll show you how to get started with your Firebase project, prepare for Xcode, and authenticate with Firebase!

What is Firebase

Firebase is a service provided by Google for app developers that allows you to easily implement login authentication and online data management.

You can develop apps that require authentication functions and databases, and chat apps!

Install the Firebase SDK

To use Firebase for iOS development, first install the Firebase SDK in your project file!

1. Create a Firebase project

Go to Firebase (https://console.firebase.google.com/) and click ** Add Project ** Firebase Enter any project name and ** continue ** Decide whether to enable Google Analytics and ** continue ** (Recommendation is valid) Select a Google Analytics account (usually the Default Account for Firebase) and ** create a project ** You will be redirected to a page like this. This completes the project creation. image.png

2. Register your app with Firebase

From here, we will link the Firebase and Xcode project files.

Click the ** iOS ** button on the Firebase console. When the option screen opens, copy the Bundle Identifier of the Xcode project in the iOS bundle ID field and click ** Add App ** image.png Download GoogleService-Info.plist and add it to your Xcode project by dragging and dropping. Once you've done that, close Xcode.

3. Add the Firebase SDK to your app

There are several ways to add the Firebase SDK, but this time we'll add it using CocoaPods, which is the simplest and easiest.

Go to the Xcode project folder in Finder, right click> Services> ** New Terminal in Folder ** to open the terminal image.png Type pod init in the terminal and press Enter. After waiting for a while, a file called Podfile will appear in the project folder, so open it with an editor. Once opened, under # Pods for Firebase App, Add pod'Firebase/Analytics' and save. Once saved, go back to your terminal and run pod install. This will generate a **. Xcworkspace file ** in your project folder. For future development, we will use **. Xcworkspace files ** instead of .xcodeproj files.

4. Add initialization code

Open the ** .xcworkspace file **, open ** AppDelegate.swift **, and add import Firebase andFIRApp.configure (). image.png

Now you're ready to develop with Firebase!

Add login function with Firebase UI

You can easily implement the login function using FirebaseUI! This time, we will implement ** Sign in with Google ** and ** Sign in with Apple **. (When releasing an app, it is necessary to implement Sign in with Apple for apps that have a third-party login linkage! Https://qiita.com/akatsuki174/items/77734fb95b74cfa5cdad)

1. Update Podfile

Open the podfile, write pod'FirebaseUI', and run pod install in the terminal as you did when preparing.

2. Enable Authentication on Firebase

Select Authentication from the Firebase console and click ** Start ** Sign in with Apple Select Apple from the Sign-in method, check ** Enable **, and click ** Save **. Sign in with Google Select Google from the Sign-in method, check ** Enable **, enter ** project public name ** and ** support email **, and click ** Save ** To do. Then copy the REVERSED_CLIENT_ID in the GoogleService-Info.plist and paste it into your URL scheme. Add the URL scheme from the *** button ** at the bottom left. image.png

Open AppDelegate.swift and add the following

AppDelegate.swift


func application(_ app: UIApplication, open url: URL,
                     options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
        let sourceApplication = options[UIApplication.OpenURLOptionsKey.sourceApplication] as! String?
        if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false {
            return true
        }
        // other URL handling goes here.
        return false
    }

This completes the preparation!

3. Coding

image.png Segue's Identifer is toNextView

ViewController.swift


import UIKit
import FirebaseUI

class ViewController: UIViewController, FUIAuthDelegate {

    var authUI: FUIAuth { get { return FUIAuth.defaultAuthUI()!}}
    //Select a provider to authenticate your login
    let providers: [FUIAuthProvider] = [FUIOAuth.appleAuthProvider(),FUIGoogleAuth()]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.authUI.delegate = self
        self.authUI.providers = providers
    }

    @IBAction func LoginButtonTapped(){
        //Get View of Firebase UI
        let authViewController = self.authUI.authViewController()
        //View View in Firebase UI
        self.present(authViewController, animated: true, completion: nil)
    }
    
    public func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?){
        if error == nil {
            //Authentication successful
            self.performSegue(withIdentifier: "toNextView", sender: nil)
        } else {
            //Authentication failure
            print(error!)
        }
    }

}

Complete! !! You have now added the ** login function **! 88035b1020c6307c3fdc85f909988d5b.gif

at the end

With Firebase, you can throw all the annoying parts of online data management, authentication, etc. into Firebase, so you can implement your app quickly and easily! Master Firebase and create great apps!

Recommended Posts

[Swift] First Firebase
First Swift Lint
Swift Combine First step
Let's use Swift Firebase Firebase Auth
[Swift] Type type-Class first part-
First post
[Swift 5] Implementation of membership registration with Firebase
Firebase MLKit
[Swift] Closure
First post
How to overwrite Firebase data in Swift
First post
First java.util.logging