[Swift] How to install Firebase ~ How to use Realtime Database & Cloud Firestore

Preface

Since I created an app that uses Firebase, I made a note of how to install it in order to facilitate the introduction of projects using Firebase in the future. The second half also describes how to create and use the Realtime Database and Cloud Firestore databases.

① Project creation procedure

1. Access the Firebase console and press ** Create Project **

1.png

2. Create a project

Enter any project name in project name Press the ** Continue ** button スクリーンショット 2020-10-04 17.44.54.png

3. Analytics configuration

Keep the default ** Continue ** button 3.png

Set Analytics Region to Japan Check all Data sharing settings and Google Analytics Terms of Service ** Create project ** Press button 4.png

4. Completion of creation

Press the ** Continue ** button スクリーンショット 2020-10-04 17.45.44.png

② Add application

1. Install a configuration file for using Firebase in your iOS app

Select ** iOS ** from the project console 1.png

2. Add Firebase to your iOS app

  1. ʻiOS Add Firebase to iOS Bundle ID Enter the bundle ID of the iOS app App nickname ʻApp Store ID` is optional After inputting, press the ** Register App ** button 2.png

  2. Download ** GoogleService-info.plist and copy it ** to the root folder of your project. After copying, press the ** Next ** button 3.png

  3. Follow the instructions provided to install the Firebase SDK via CocoaPods. Press the ** Next ** button when the installation is complete 4.png

  4. Select Swift. Open ʻAppDelegate.swift` on the iOS app side. Add the following two places. After filling in, press the ** Next ** button. 5.png

  5. Start the app and check the communication.

・ Checking communication 6.png

・ Successful communication 7.png

③ Create a database with Realtime Database

  1. Open the created project 1.png

  2. Select Realtime Database and press the Create Database button 2.png

  3. Select Start in lock mode and press the ** Enable ** button 3.png

  4. From the ** rule **, set read`` write to true and press the ** publish ** button. 4.png

  5. Install the SDK for using Realtime Database via CocoaPod

pod 'Firebase/Database'
  1. Copy the code below into ViewController.Swift and run the app
import UIKit
import FirebaseDatabase

class ViewController: UIViewController {
    
    var databaseRef: DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Get a shared instance of Firebase
        databaseRef = Database.database().reference()
        
        //Callback function after data storage processing
        let resultCallback = { (error: Error?, ref: DatabaseReference) -> () in
            print(#function)
        }
        
        //Data storage
        self.databaseRef.child("Root").childByAutoId().setValue("TestValue", withCompletionBlock: resultCallback)
    }
}
  1. If you check the Realtime Database console, you can see that the data has been saved. 7.png

④ Create a database with Cloud Firestore

  1. Open the created project 1.png

  2. Select Cloud Firestore and press the Create Database button 1.png

  3. Select Start in production mode and press the ** Next ** button. 2.png

  4. Select nam5 (us-central) from ** Cloud Firestore ** and press the ** Enable ** button. 3.png

  5. From the ** rule **, set read`` write to true and press the ** publish ** button. 4.png

  6. Install the SDK for using Cloud Firestore via CocoaPod

pod 'Firebase/Firestore'
  1. Select ** Start Collection ** from Data 5.png

  2. Enter ʻusers` in the collection ID and press the ** Next ** button. 6.png

  3. Enter the following contents and press the ** Save ** button Document ID: ʻuser_dataField:nameType:stringValue:test user` 7.png

8.png

  1. Copy and execute the following code into ViewController.swift and the username will be output to the console
import UIKit
import FirebaseFirestore

class ViewController: UIViewController {
    
    //Shared instance of Firestore
    var databaseStore: Firestore!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Firestore shared instance
        self.databaseStore = Firestore.firestore()
        
        //Get data from the users collection
        databaseStore.collection("users").document("user_data").getDocument{ (document, error) in
            
            if let document = document {
                if let data = document.data() {
                    //Get username
                    let name: String = (data["name"] as? String)!
                    
                    print(name)
                }
            } else {
                print("error")
            }
        }
    }
}

Recommended Posts

[Swift] How to install Firebase ~ How to use Realtime Database & Cloud Firestore
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
Easy to use Cloud Firestore (Android)
Use Firebase Realtime Database with Cocos Creator.
[Swift] How to use Tab Bar Controller
How to overwrite Firebase data in Swift
[Swift] How to use one option alert
Note how to use Swift super basic TableView
How to use Alibaba Cloud LOG Java Producer
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to install MySQL
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to install ngrok
[Swift] How to get the document ID of Firebase
[Java] How to use Map
How to use Chain API
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
How to use java class
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use Ubuntu Server 20.10 (Groovy Gorilla) in Sakura's cloud
Let's use Swift Firebase Firebase Auth
How to use String [] args
[Java] How to use string.format
How to install Boots Faces
How to use rails join
How to use Java Map
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
How to use Java variables