我一直试图解决这个问题几天,而且似乎无法找到有效的解决方案。
问题是:当我尝试在我的应用中创建帐户时,它会自动创建一个帐户而不输入任何信息。所以基本上用户没有提供任何信息,并且仍在创建帐户。
我目前正在使用Firebase
以下是该视图的代码:
import UIKit
import Firebase
class createAccountVC: UIViewController {
private let RIDER_SEGUE = "createAccountSegue"
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var confirmPasswordTextField: UITextField!
@IBOutlet weak var createAccountButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
createAccountButton.titleLabel?.numberOfLines = 2
createAccountButton.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
}
private func alertTheUser(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(ok)
present(alert, animated: true, completion: nil)
}
@IBAction func signUp(_ sender: AnyObject) {
if emailTextField.text != "" && passwordTextField.text != "" && confirmPasswordTextField.text != "" {
AuthProvider.Instance.signUp(withEmail: emailTextField.text!, password: passwordTextField.text!, loginHandler: { (message) in
if message != nil {
self.alertTheUser(title: "Problem with creating a new user.", message: message!)
} else {
GroomrHandler.Instance.rider = self.emailTextField.text!
self.saveUserInfo(email: self.emailTextField.text!, password: self.passwordTextField.text!)
self.emailTextField.text = ""
self.passwordTextField.text = ""
self.confirmPasswordTextField.text = ""
//self.performSegue(withIdentifier: self.RIDER_SEGUE, sender: nil)
// save password to keychain
}
})
} else {
alertTheUser(title: "Email and password are required.", message: "Please enter email and password in the text fields.")
}
}
func saveUserInfo(email: String, password: String) {
//save login info in app
UserDefaults.standard.set(email, forKey: "email")
}
}
提前谢谢!不要惹我生气,我还在学习XD
编辑:这是AuthProvider文件:
import Foundation
import Firebase
typealias LoginHandler = (_ msg: String?) -> Void
struct LoginErrorCode {
static let INVALID_EMAIL = "Invalid email address, please provide a real email address."
static let WRONG_PASSWORD = "Wrong password, please enter the correct password."
static let PROBLEM_CONNECTING = "Problem connecting to database, please try later."
static let USER_NOT_FOUND = "User not found, please register."
static let EMAIL_ALREADY_IN_USE = "Email already in use, please use another email."
static let WEAK_PASSWORD = "Password should be at least 6 characters long."
}
class AuthProvider {
private static let _instance = AuthProvider()
static var Instance: AuthProvider {
return _instance
}
func login(withEmail: String, password: String, loginHandler: LoginHandler?) {
Auth.auth().signIn(withEmail: withEmail, password: password, completion: { (user, error) in
if error != nil {
self.handleErrors(err: error! as NSError, loginHandler: loginHandler)
} else {
loginHandler?(nil)
}
})
} // login func
func signUp(withEmail: String, password: String, loginHandler: LoginHandler?) {
Auth.auth().createUser(withEmail: withEmail, password: password, completion: { (user, error) in
if error != nil {
self.handleErrors(err: error! as NSError, loginHandler: loginHandler)
} else {
if user?.uid != nil {
// login the user
self.login(withEmail: withEmail, password: password, loginHandler: loginHandler)
}
}
})
} // sign up func
func logOut() -> Bool {
if Auth.auth().currentUser != nil {
do {
try Auth.auth().signOut()
return true
} catch {
return false
}
}
return true
}
private func handleErrors(err: NSError, loginHandler: LoginHandler?) {
if let errCode = AuthErrorCode(rawValue: err.code) {
switch errCode {
case .wrongPassword:
loginHandler?(LoginErrorCode.WRONG_PASSWORD)
break
case .invalidEmail:
loginHandler?(LoginErrorCode.INVALID_EMAIL)
break
case .userNotFound:
loginHandler?(LoginErrorCode.USER_NOT_FOUND)
break
case .emailAlreadyInUse:
loginHandler?(LoginErrorCode.EMAIL_ALREADY_IN_USE)
break
case .weakPassword:
loginHandler?(LoginErrorCode.WEAK_PASSWORD)
break
default:
loginHandler?(LoginErrorCode.PROBLEM_CONNECTING)
break
}
}
}
} // class
答案 0 :(得分:0)
我在注册用户方面有一些经验,这是我的代码,你可以尝试一下。希望它有所帮助。
这在User对象中:
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
user?.sendEmailVerification(completion: nil)
let storageRef = Storage.storage().reference().child("usersProfilePics").child(user!.uid)
let imageData = UIImageJPEGRepresentation(profilePic, 0.1)
storageRef.putData(imageData!, metadata: nil, completion: { (metadata, err) in
if err == nil {
let path = metadata?.downloadURL()?.absoluteString
let values = ["name": withName, "email": email, "profilePicLink": path!]
Database.database().reference().child("users").child(userType).child((user?.uid)!).child("credentials").updateChildValues(values, withCompletionBlock: { (errr, _) in
if errr == nil {
let userInfo = ["email" : email, "password" : password, "type" : userType]
UserDefaults.standard.set(userInfo, forKey: "userInformation")
completion(true)
}
})
}
})
}
else {
completion(false)
}
})
这里我正在调用User对象的方法:
User.registerUser(withName: self.registerNameField.text!, email: self.registerEmailField.text!, password: self.registerPasswordField.text!, profilePic: self.profilePicView.image!, forUserType: UserType.psychologist) { [weak weakSelf = self] (status) in
DispatchQueue.main.async {
weakSelf?.showLoading(state: false)
for item in self.inputFields {
item.text = ""
}
if status == true {
weakSelf?.pushTomainView()
weakSelf?.profilePicView.image = UIImage.init(named: "profile pic")
} else {
for item in (weakSelf?.waringLabels)! {
item.isHidden = false
}
}
}
}
答案 1 :(得分:0)
var abcd: String?
if abcd != "" {
print("hello")
}
以上代码在控制台中打印hello
。您的代码中也发生了类似的问题。条件abcd != ""
只测试空,而不是空。您应首先解开所有文本字段文本,然后检查isEmpty
,并创建帐户。
@IBAction func signUp(_ sender: AnyObject) {
guard let email = emailTextField.text,
let password = passwordTextField.text,
let confirmPassword = confirmPasswordTextField.text,
email != "",
password != "",
confirmPassword != "" else {
alertTheUser(title: "Email and password are required.", message: "Please enter email and password in the text fields.")
}
AuthProvider.Instance.signUp(withEmail: email, password: password, loginHandler: { (message) in
if message != nil {
self.alertTheUser(title: "Problem with creating a new user.", message: message!)
} else {
GroomrHandler.Instance.rider = email
self.saveUserInfo(email: email, password: password)
self.emailTextField.text = ""
self.passwordTextField.text = ""
self.confirmPasswordTextField.text = ""
//self.performSegue(withIdentifier: self.RIDER_SEGUE, sender: nil)
// save password to keychain
}
})
}