When I hit my sign up button without uploading an image I get a crash. If I upload a photo without filling out the rest of the textFields I get a pop up alert. How can I get the same alert when there is no photo being uploaded? The error I get is
fatal error: unexpectedly found nil while unwrapping an Optional value
@IBAction func signUpAction(_ sender: Any) {
let email = emailTextField.text!.lowercased()
let finalEmail = email.trimmingCharacters(in: .whitespacesAndNewlines)
let location = locationTextField.text!
let biography = biographyTextField.text!
let username = usernameTextField.text!
let password = passwordTextField.text!
let firstLastName = firstLastNameTextField.text!
let pictureData = UIImageJPEGRepresentation(self.userImageView.image!, 0.70)
if finalEmail.isEmpty || location.isEmpty || biography.isEmpty || username.isEmpty || password.isEmpty {
self.view.endEditing(true)
let alertController = UIAlertController(title: "OOPS", message: " You must fill all the fields", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}else {
self.view.endEditing(true)
authService.signUP(username: username, email: finalEmail, location: location, biography: biography, password: password, pictureData: pictureData as NSData!)
}
}
var authService = AuthService()
struct AuthService{
var dataBaseRef: DatabaseReference!{
return Database.database().reference()
}
var storageRef: StorageReference!{
return Storage.storage().reference()
}
func signUP(username: String, email: String, location: String, biography: String, password: String, pictureData: NSData!) {
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error == nil{
self.setUserInfo(user: user, username: username, location: location, biography: biography, password: password, pictureData: pictureData)
}else{
print(error?.localizedDescription)
}
}
}
private func setUserInfo(user: User!, username: String, location: String, biography: String, password: String, pictureData: NSData!){
let imagePath = "profileImage\(user.uid)/userPic.jpg"
let imageRef = storageRef.child(imagePath)
let metaData = StorageMetadata()
metaData.contentType = "image/jpeg"
imageRef.putData(pictureData as Data, metadata: metaData){(newMetaData, error)
in
if error == nil{
let changeRequest = user.createProfileChangeRequest()
changeRequest.displayName = username
if let photoURL = newMetaData!.downloadURL(){
changeRequest.photoURL = photoURL
}
changeRequest.commitChanges(completion: { (error) in
if error == nil{
self.saveUserInfo(user: user, username: username, location: location, biography: biography, password: password)
print("user info set")
}else{
print(error?.localizedDescription)
}
})
}else{
print(error?.localizedDescription)
}
}
}
private func saveUserInfo(user: User!, username: String, location: String, biography: String, password: String){
let userInfo = ["email": user.email!, "username": username, "location": location, "biography": biography, "uid": user.uid, "photoURL": String(describing: user.photoURL!)]
let userRef = dataBaseRef.child("users").child(user.uid)
userRef.setValue(userInfo) { (error, ref) in
if error == nil{
print("USER SAVED")
self.logIn(email: user.email!, password: password)
}else{
print(error?.localizedDescription)
}
}
}
func logIn(email: String, password: String){
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if error == nil {
if let user = user {
print("\(user.displayName!) has been signed in")
let appDel : AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDel.logUser()
}else{
print(error?.localizedDescription)
}
}
}
}
}