Итак, я работаю над страницей регистрации в моем приложении. Прямо сейчас я обнаруживаю ошибки и показываю предупреждения, если что-то пошло не так.
Это простой пример того, как я это делаю:
if username == "" {
errorArray.append("didnt go through")
} else {
errorArray.append("made it through")
}
if errorArray.contains("didnt go through") {
accessGiven = false
} else {
accessGiven = true
}
Кстати, яиспользуя accessGiven
в качестве переменной, которая изменяется во всем коде. Кроме того, фактический код использует .append(1)
или .append(2)
.
Вот полный код:
@IBAction func nextPressed(_ sender: Any) {
if imageView.image == nil {
alerts(title: "Ooops...", message: "Please select a profile picture.")
accessGiven = false
accessArray.append(+2)
} else {
accessGiven = true
accessArray.append(+1)
}
if usernameField.text == "", emailField.text == "", password.text == "", comPwField.text == "" {
alerts(title: "Oops...", message: "Please fill out the fields.")
accessGiven = false
accessArray.append(+2)
} else {
accessGiven = true
accessArray.append(+1)
}
if usernameField.text == "" {
alerts(title: "Oops...", message: "Username can't be blank.")
accessGiven = false
accessArray.append(+2)
} else {
accessGiven = true
accessArray.append(+1)
}
Auth.auth().fetchProviders(forEmail: self.emailField.text!, completion: { (stringArray, error) in
if error != nil {
print(error!)
self.accessGiven = false
self.accessArray.append(+2)
} else {
if stringArray == nil {
print("No active account using this email.")
self.accessGiven = true
self.accessArray.append(+1)
//self.performSegue(withIdentifier: "segue.SignUp.toTabBar", sender: nil)
} else {
print("There is an active account using this E-mail")
self.alerts(title: "Oops...", message: "There is an active account using this E-Mail.")
self.accessGiven = false
self.accessArray.append(+2)
}
}
})
// Passwords
if password.text == comPwField.text {
print("Passwords match.")
accessGiven = true
self.accessArray.append(+1)
} else {
print("Passwords don't match.")
accessGiven = false
self.accessArray.append(+2)
}
let email = isValidEmail(testStr: emailField.text!)
if email == false {
alerts(title: "Oops...", message: "Invalid E-Mail.")
accessGiven = false
self.accessArray.append(+2)
} else {
accessGiven = true
self.accessArray.append(+1)
}
let passwordFieldTextLength = password.text!.characters.count
let comPwFieldTextLength = comPwField.text!.characters.count
if passwordFieldTextLength < 6 && comPwFieldTextLength < 6 {
print("Pssword isn't longer than 6 characters!")
alerts(title: "Oops...", message: "Password isn't longer than 6 characters.")
accessGiven = false
self.accessArray.append(+2)
} else {
accessGiven = true
self.accessArray.append(+1)
}
ref.child("users")
.queryOrdered(byChild:"username")
.queryEqual(toValue: self.usernameField.text!)
.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() == false {
print("Username not in use.")
self.accessGiven = true
self.accessArray.append(+1)
} else {
print("Username already in use.")
self.alerts(title: "Oops", message: "Username already taken.")
self.accessGiven = false
self.accessArray.append(+2)
}
})
if accessArray.contains(2) {
print("Access not Given.")
} else {
print("Access Given")
Auth.auth().createUser(withEmail: emailField.text!, password: password.text!, completion: { (user,
error) in
if let error = error {
print(error.localizedDescription)
}
if let user = user {
let changeRequest = Auth.auth().currentUser!.createProfileChangeRequest()
changeRequest.displayName = self.usernameField.text!
changeRequest.commitChanges(completion: nil)
let imageRef = self.userStorage.child("\(user.uid).jpg")
let data = UIImageJPEGRepresentation(self.imageView.image!, 0.5)
let uploadTask = imageRef.putData(data!, metadata: nil, completion: { (metadata, err) in
if err != nil {
print(err!.localizedDescription)
}
imageRef.downloadURL(completion: { (url, er) in
if er != nil {
print(er!.localizedDescription)
}
if let url = url {
let userInfo: [String : Any] = ["uid" : user.uid,
"username" : self.usernameField.text!,
"email" : self.emailField.text!,
"urlToImage" : url.absoluteString]
self.ref.child("users").child(user.uid).setValue(userInfo)
// let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "usersVC")
//
// self.present(vc, animated: true, completion: nil)
}
})
})
uploadTask.resume()
}
})
}
print(accessArray)