cd /Users/[username]/Desktop/[Project name]/
If you are worried that you can move it, execute the pwd
command.
After moving, create a pod file.
pod init
When you run the command, a podfile
is created.
To check, execute the ls
command.
If it was created properly
open podfile
Open the file with. When you open it, edit it like this.
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'FirebaseTest' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for FirebaseTest
//add to
pod 'Firebase/Auth'
end
Return to the terminal again. When you get back, type the following command.
pod intall
Now you can use FirebaseAuth
.
At the terminal
open [Project name].xcworkspace
Please hit.
You can open it from the Finder.
The extension is .xcworkspace
instead of .xcodeproj
, so be careful not to make a mistake.
Once opened, let's edit the ViewController
.
ViewController.swift
class ViewController: UIViewController {
private var emailFiled = UITextField()
private var passwordField = UITextField()
private let authButton: UIButton = {
let button = UIButton()
button.setTitle("Registration", for: .normal)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
emailFiled = initField(placeholder: "email")
passwordField = initField(placeholder: "password")
authButton.addTarget(self, action: #selector(didTapAuthButton), for: .touchUpInside)
view.addSubview(emailFiled)
view.addSubview(passwordField)
view.addSubview(authButton)
emailFiled.frame = .init(x: 50, y: 100, width: view.frame.size.width-100, height: 40)
passwordField.frame = .init(x: 50, y: 160, width: view.frame.size.width-100, height: 40)
authButton.frame = .init(x: 50, y: 220, width: view.frame.size.width-100, height: 40)
}
@objc func didTapAuthButton() {
}
private func initField(placeholder: String) -> UITextField {
let field = UITextField()
field.placeholder = placeholder
field.autocorrectionType = .no
field.autocapitalizationType = .none
field.layer.borderWidth = 1
field.layer.borderColor = UIColor.black.cgColor
if placeholder == "password" {
//Hide the characters you type
field.isSecureTextEntry = true
}
return field
}
}
Let's set the View like this.
I'm sorry to have kept you waiting.
We will use FirebaseAuth
.
Write the process in didTapAuthButton
.
Write only the changes.
import FirebaseAuth
class ViewController: UIViewController {
@objc func didTapAuthButton() {
guard let email = emailFiled.text, let password = passwordField.text else {return}
Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
guard let user = result?.user, err == nil else {
print("error: ", err!)
return
}
let userEmail = user.email
print("email: ", userEmail ?? "")
}
}
}
This is the only way to create a new user
If there is nothing you want to do after createUser
, you only needAuth.auth (). createUser (with Email: email, password: password)
.
Well, I don't think it's possible to use the closure with the contents empty.
Let's log in and see the code immediately.
@objc func didTapAuthButton() {
guard let email = emailFiled.text, let password = passwordField.text else {return}
Auth.auth().signIn(withEmail: email, password: password)
}
This is all you need to do the login function. It's really easy.
You can also log out like this.
@objc func didTapAuthButton() {
do {
try Auth.auth().signOut()
} catch(let err) {
print(err)
}
}
Exception handling occurs when logging out, so what is exception handling? If you think that, please refer to here. Introduction to Swift 4.0 Error Handling
You can easily implement other functions such as logging in with a phone number, so please check it out. Up to here for this time.
Recommended Posts