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!
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!
To use Firebase for iOS development, first install the Firebase SDK in your project file!
Go to Firebase (https://console.firebase.google.com/) and click ** Add Project ** 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.
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 ** Download GoogleService-Info.plist and add it to your Xcode project by dragging and dropping. Once you've done that, close Xcode.
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
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.
Open the ** .xcworkspace file **, open ** AppDelegate.swift **, and add import Firebase
andFIRApp.configure ()
.
Now you're ready to develop with Firebase!
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)
Open the podfile, write pod'FirebaseUI'
, and run pod install
in the terminal as you did when preparing.
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.
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!
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 **!
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