ios - 成功登录 Firebase 后尝试执行 segue
问题描述
我想在登录成功后执行 segue,否则我希望视图保留在登录屏幕上。我这样做有点麻烦。对 Swift 来说相当陌生,所以请原谅我的任何无知。非常感谢您的参与。
这是我的 LogInViewController
import UIKit
import FirebaseAuth
import Firebase
class LogInViewController: UIViewController {
let firebaseService = FirebaseService()
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func logInAction(_ sender: Any) {
firebaseService.signIn(email: emailTextField.text!, password: passwordTextField.text!)
if Auth.auth().currentUser != nil {
afterSuccessfulLogin()
} else {
print("Wrong Account Info")
}
}
func afterSuccessfulLogin() {
performSegue(withIdentifier: "afterLogin", sender: self)
print("\(Auth.auth().currentUser!.displayName!) has logged in!")
}
@IBAction func unwindToLogIn(storyboard: UIStoryboardSegue){
}
}
这是我的 FirebaseCalls.swift
import Firebase
import FirebaseAuth
import FirebaseStorage
import FirebaseDatabase
struct FirebaseService {
var dbRef : DatabaseReference! {
return Database.database().reference()
}
var storageRef : StorageReference! {
return Storage.storage().reference()
}
private func saveInfo(user: User?, username: String, password: String, city: String, state: String, bio: String) {
//Create User Dictionary Info
let userInfo = ["email": user?.email!, "username": username, "city": city, "state": state, "bio": bio, "uid": user?.uid, "photoUrl": String(describing: user!.photoURL!)]
//Create User Reference
let userRef = dbRef.child("users").child((user?.uid)!)
// Save User Info in Database
userRef.setValue(userInfo)
//SIGN IN THE USER
signIn(email: (user?.email!)!, password: password)
}
func signIn(email: String, password: String) {
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
if let user = user {
print("\(user.displayName!) has signed in successfully!")
}
} else {
print(error!.localizedDescription)
}
})
}
private func setUserInfo(user: User?, username: String, password: String, city: String, state: String, bio: String, data: NSData!) {
//CREATE PATH FOR USER IMAGE
let imagePath = "profileImage\(user!.uid)/userPic.jpg"
//CREATE IMAGE REFERANCE
let userImageRef = storageRef.child(imagePath)
//Create MetaData for the Image
let metaData = StorageMetadata()
metaData.contentType = "image/jpeg"
userImageRef.putData(data as Data, metadata: metaData) { (metaData, error) in
if error == nil {
let changeRequest = user?.createProfileChangeRequest()
changeRequest?.displayName = username
changeRequest?.photoURL = metaData!.downloadURL()
changeRequest?.commitChanges(completion: { (error) in
if error == nil {
self.saveInfo(user: user, username: username, password: password, city: city, state: state, bio: bio)
}else {
print(error!.localizedDescription)
}
})
}else {
print(error!.localizedDescription)
}
}
}
func resetPassword(email: String){
Auth.auth().sendPasswordReset(withEmail: email, completion: { (error) in
if error == nil {
print("An email with information on how to Reset your password has been sent.")
} else {
print(error!.localizedDescription)
}
})
}
//Save the user Image in the Firebase Storage File
func signUp(email: String, username: String, password: String, city: String, state: String, bio: String, data: NSData!) {
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
self.setUserInfo(user: user, username: username, password: password, city: city, state: state, bio: bio, data: data)
} else {
print(error!.localizedDescription)
}
})
}
}
解决方案
给这个方法添加一个补全
func signIn(email: String, password: String,completion:@escaping(_ finished:Bool)->void) {
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
if let user = user {
print("\(user.displayName!) has signed in successfully!")
completion(true)
}
} else {
print(error!.localizedDescription)
completion(false)
}
})
}
//
@IBAction func logInAction(_ sender: Any) {
firebaseService.signIn(email: emailTextField.text!, password: passwordTextField.text! , { (finished) in
if finished {
if Auth.auth().currentUser != nil {
afterSuccessfulLogin()
} else {
print("Wrong Account Info")
}
}
})
推荐阅读
- vue.js - Vue.js。如何从字符串(道具)制作函数?
- blazor - 如何防止 MarkupString 在 Blazor 中自动关闭标签
- html - ngModel 属性在我的子 html 组件中不起作用
- r - Thesisdown:加载 huxtable 包和创建表格时,编织到 PDF 失败
- visual-studio-code - 如何使用图标字体
- php - 如何为每一列显示一行?
- python - OpenCV 特征对到点云
- python - 引用python中已删除的对象
- codeigniter - 如何从codeigniter中的分页中提取特定的链接值(页码)?
- javascript - 如何在 JS 中回调函数