gpt4 book ai didi

Swift:如何在创建新用户之前检查用户的电话号码是否已经在 Firebase 数据库中

转载 作者:行者123 更新时间:2023-11-28 07:55:21 25 4
gpt4 key购买 nike

我几个月前才开始使用 Swift,为了帮助我更好地学习这门语言,我正在创建一个聊天应用程序。对于登录方法,我使用的是电话号码方法。我已经创建了入职流程,但我想实现一些让我检查用户是否已经使用该电话号码创建帐户的东西。如果有,我想将它们转到主视图 Controller ,跳过入职 View Controller 。

这是我的电话验证 View Controller 代码(一个用于输入电话号码,另一个用于输入发送到用户电话的代码):

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase

class PhoneVerification: UIViewController {

//MARK: Properties
@IBOutlet weak var phoneNumber: UITextField!
@IBOutlet weak var code: UITextField!
@IBOutlet weak var verifyCodeImage: UIButton!
@IBOutlet weak var sendCodeImage: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


//MARK: Format phone text field
var phoneFormatter = PhoneNumberFormatter()
@IBAction func formatPhoneNumber(_ sender: UITextField) {
sender.text = phoneFormatter.format(sender.text!, hash: sender.hash)
}

//MARK: When send secret code button is pressed

@IBAction func sendCode(_ sender: Any) {
let submitPhoneNumber = "+1" + phoneNumber.text!
if submitPhoneNumber.count > 9{
PhoneAuthProvider.provider().verifyPhoneNumber(submitPhoneNumber, uiDelegate: nil) {(verificationID, error) in
if error != nil {
print(error!)
}else{
UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
self.performSegue(withIdentifier: "phoneCode", sender: self)
}
}
}else{
let phoneNumAlert = UIAlertController(title: "Please enter your phone number", message: "You must enter your phone number to continue.", preferredStyle: .alert)

phoneNumAlert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))

self.present(phoneNumAlert, animated: true)
}
}
let verificationID = UserDefaults.standard.string(forKey: "authVerificationID")

//MARK: When verify code button is pressed
@IBAction func verifyCode(_ sender: Any) {
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationID!,
verificationCode: code.text!)

//This is where the user is signed in if the verification code is correct
Auth.auth().signIn(with: credential) { (user, error) in
if let error = error {

let invalidCodeAlert = UIAlertController(title: "That code is incorrect", message: "Please input the correct code", preferredStyle: .alert)

invalidCodeAlert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))

self.present(invalidCodeAlert, animated: true)
print(error)
return
}
//MARK: User is signed in
print("Phone number: \(String(describing: user?.phoneNumber))")
self.performSegue(withIdentifier: "accountCreated", sender: self)
}
}

}

非常感谢所有帮助!谢谢!

最佳答案

假设您将用户数据保存在 users ref 中。当用户登录时检查他们是否有任何数据,如果他们有那么他们是现有用户,如果他们没有那么它是一个新帐户:

Auth.auth().signIn(with: credential, completion: { [weak self](authDataResult, error) in

if let error = error { return }

guard let safeAuthDataResult = authDataResult else { return }

// 1. get the signed in user's userId
let userId = safeAuthDataResult.user.uid

// 2. check to see if their userId exists at the user's path
let usersRef = Database.database().reference().child("users").child(userId)
usersPublicDataRef.observeSingleEvent(of: .value, with: { (snapshot) in

// 3. this is a new user
if !snapshot.exists() {

// *** what you should do here is update the user's ref with some sort of data

} else {

// 4. this is an existing user
}
})
})

关于Swift:如何在创建新用户之前检查用户的电话号码是否已经在 Firebase 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48274621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com