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.
Enter any project name in project name
Press the ** Continue ** button
Keep the default ** Continue ** button
Set Analytics Region
to Japan
Check all Data sharing settings and Google Analytics Terms of Service
** Create project ** Press button
Press the ** Continue ** button
Select ** iOS ** from the project console
ʻ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
Download ** GoogleService-info.plist and copy it ** to the root folder of your project. After copying, press the ** Next ** button
Follow the instructions provided to install the Firebase SDK via CocoaPods. Press the ** Next ** button when the installation is complete
Select Swift
.
Open ʻAppDelegate.swift` on the iOS app side. Add the following two places.
After filling in, press the ** Next ** button.
Start the app and check the communication.
・ Checking communication
・ Successful communication
Open the created project
Select Realtime Database
and press the Create Database
button
Select Start in lock mode
and press the ** Enable ** button
From the ** rule **, set read`` write
to true
and press the ** publish ** button.
Install the SDK for using Realtime Database via CocoaPod
pod 'Firebase/Database'
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)
}
}
Open the created project
Select Cloud Firestore
and press the Create Database
button
Select Start in production mode
and press the ** Next ** button.
Select nam5 (us-central)
from ** Cloud Firestore ** and press the ** Enable ** button.
From the ** rule **, set read`` write
to true
and press the ** publish ** button.
Install the SDK for using Cloud Firestore via CocoaPod
pod 'Firebase/Firestore'
Select ** Start Collection ** from Data
Enter ʻusers` in the collection ID and press the ** Next ** button.
Enter the following contents and press the ** Save ** button
Document ID: ʻuser_dataField:
nameType:
stringValue:
test user`
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