Hallo Leute. Bitte nicht böse sein wenn ich etwas falsch formuliere. Bin noch am Anfang von der Programmiersprache. Habe ein Problem und weiss nicht weiter.
Onkel Google spuckt nichts raus.
import UIKit
import Firebase
class SignUpController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
lazy var plusImageView: UIImageView = {
let imageView = UIImageView()
imageView.image =imageLiteral(resourceName: "plus_photo")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handlePlusPhoto)))
return imageView
}()
@objc func handlePlusPhoto() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
self.plusImageView.image = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
self.plusImageView.image = originalImage
}
picker.dismiss(animated: true, completion: nil)
}
let emailTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Email"
tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
tf.borderStyle = .roundedRect
tf.font = UIFont.systemFont(ofSize: 14)
tf.keyboardType = .emailAddress
tf.addTarget(self, action: #selector(handleInputChange), for: .editingChanged)
return tf
}()
let usernameTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Username"
tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
tf.borderStyle = .roundedRect
tf.font = UIFont.systemFont(ofSize: 14)
tf.keyboardType = .emailAddress
tf.addTarget(self, action: #selector(handleInputChange), for: .editingChanged)
return tf
}()
let passwordTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Password"
tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
tf.borderStyle = .roundedRect
tf.font = UIFont.systemFont(ofSize: 14)
tf.isSecureTextEntry = true
tf.addTarget(self, action: #selector(handleInputChange), for: .editingChanged)
return tf
}()
let signUpButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Sign Up", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.backgroundColor = UIColor.lightGray
button.isEnabled = false
button.addTarget(self, action: #selector(handleSignup), for: .touchUpInside)
button.layer.cornerRadius = 5
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
return button
}()
let noAccountButton: UIButton = {
let button = UIButton(type: .system)
let attributedString = NSMutableAttributedString(string: "Already have an Account? ", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: UIColor.lightGray])
attributedString.append(NSMutableAttributedString(string: "Login", attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: ColorCodes.instagramBlue]))
button.setAttributedTitle(attributedString, for: .normal)
button.addTarget(self, action: #selector(handleShowSignUp), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isNavigationBarHidden = true
view.backgroundColor = .white
view.addSubview(plusImageView)
view.addSubview(emailTextField)
view.addSubview(usernameTextField)
view.addSubview(passwordTextField)
view.addSubview(signUpButton)
view.addSubview(noAccountButton)
plusImageView.anchor(top: view.topAnchor, left: nil, bottom: nil, right: nil, paddingTop: 50, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 150, height: 150)
plusImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
plusImageView.layer.cornerRadius = 150/2
emailTextField.anchor(top: plusImageView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 25, paddingLeft: 25, paddingBottom: 0, paddingRight: 25, width: 0, height: 40)
usernameTextField.anchor(top: emailTextField.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 8, paddingLeft: 25, paddingBottom: 0, paddingRight: 25, width: 0, height: 40)
passwordTextField.anchor(top: usernameTextField.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 8, paddingLeft: 25, paddingBottom: 0, paddingRight: 25, width: 0, height: 40)
signUpButton.anchor(top: passwordTextField.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 8, paddingLeft: 25, paddingBottom: 0, paddingRight: 25, width: 0, height: 40)
if #available(iOS 11.0, *) {
noAccountButton.anchor(top: nil, left: view.leftAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 50)
} else {
// Fallback on earlier versions
noAccountButton.anchor(top: nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 50)
}
}
@objc func handleInputChange() {
if emailTextField.text?.count ?? 0 > 0 && passwordTextField.text?.count ?? 0 > 0, usernameTextField.text?.count ?? 0 > 0 {
self.signUpButton.backgroundColor = ColorCodes.instagramBlue
self.signUpButton.isEnabled = true
} else {
self.signUpButton.isEnabled = false
self.signUpButton.backgroundColor = UIColor.lightGray
}
}
@objc func handleShowSignUp() {
UIApplication.shared.statusBarStyle = .lightContent
self.navigationController?.popViewController(animated: true)
}
@objc func handleSignup() {
guard let email = emailTextField.text, let username = usernameTextField.text, let password = passwordTextField.text else { return }
signUpButton.setTitle("Loading...", for: .normal)
signUpButton.isEnabled = false
signUpButton.backgroundColor = UIColor.lightGray
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if let error = error {
print("failed to authenticate user", error)
self.signUpButton.setTitle("Sign Up", for: .normal)
self.signUpButton.backgroundColor = ColorCodes.instagramBlue
self.signUpButton.isEnabled = true
} else {
guard let image = self.plusImageView.image else { return }
guard let uploadData = UIImageJPEGRepresentation(image, 0.2) else { return }
let filename = UUID().uuidString
let storageRef = Storage.storage().reference().child("profile_images").child(filename)
storageRef.putData(uploadData, metadata: nil) { (metadata, err) in
if let err = err {
print("failed to upload image", err)
}
guard let userId = user?.user.uid else { return }
guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return } (HIER MELDET DEN FEHLER : Value of type 'StorageMetadata' has no member 'downloadURL')
let values: [String: Any] = ["username": username, "email": email, "profileImageUrl": profileImageUrl]
let userRef = Database.database().reference().child("users").child(userId)
userRef.updateChildValues(values, withCompletionBlock: { (err, _) in
if let err = err {
print("failed to upload user", err)
}
guard let mainTabBarController = UIApplication.shared.keyWindow?.rootViewController as? MainTabBarController else { return }
mainTabBarController.setupViewControllers()
self.dismiss(animated: true, completion: nil)
})
}
}
}
}
}
Onkel Google spuckt nichts raus.
import UIKit
import Firebase
class SignUpController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
lazy var plusImageView: UIImageView = {
let imageView = UIImageView()
imageView.image =imageLiteral(resourceName: "plus_photo")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handlePlusPhoto)))
return imageView
}()
@objc func handlePlusPhoto() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
self.plusImageView.image = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
self.plusImageView.image = originalImage
}
picker.dismiss(animated: true, completion: nil)
}
let emailTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Email"
tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
tf.borderStyle = .roundedRect
tf.font = UIFont.systemFont(ofSize: 14)
tf.keyboardType = .emailAddress
tf.addTarget(self, action: #selector(handleInputChange), for: .editingChanged)
return tf
}()
let usernameTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Username"
tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
tf.borderStyle = .roundedRect
tf.font = UIFont.systemFont(ofSize: 14)
tf.keyboardType = .emailAddress
tf.addTarget(self, action: #selector(handleInputChange), for: .editingChanged)
return tf
}()
let passwordTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Password"
tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
tf.borderStyle = .roundedRect
tf.font = UIFont.systemFont(ofSize: 14)
tf.isSecureTextEntry = true
tf.addTarget(self, action: #selector(handleInputChange), for: .editingChanged)
return tf
}()
let signUpButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Sign Up", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.backgroundColor = UIColor.lightGray
button.isEnabled = false
button.addTarget(self, action: #selector(handleSignup), for: .touchUpInside)
button.layer.cornerRadius = 5
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
return button
}()
let noAccountButton: UIButton = {
let button = UIButton(type: .system)
let attributedString = NSMutableAttributedString(string: "Already have an Account? ", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: UIColor.lightGray])
attributedString.append(NSMutableAttributedString(string: "Login", attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: ColorCodes.instagramBlue]))
button.setAttributedTitle(attributedString, for: .normal)
button.addTarget(self, action: #selector(handleShowSignUp), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isNavigationBarHidden = true
view.backgroundColor = .white
view.addSubview(plusImageView)
view.addSubview(emailTextField)
view.addSubview(usernameTextField)
view.addSubview(passwordTextField)
view.addSubview(signUpButton)
view.addSubview(noAccountButton)
plusImageView.anchor(top: view.topAnchor, left: nil, bottom: nil, right: nil, paddingTop: 50, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 150, height: 150)
plusImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
plusImageView.layer.cornerRadius = 150/2
emailTextField.anchor(top: plusImageView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 25, paddingLeft: 25, paddingBottom: 0, paddingRight: 25, width: 0, height: 40)
usernameTextField.anchor(top: emailTextField.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 8, paddingLeft: 25, paddingBottom: 0, paddingRight: 25, width: 0, height: 40)
passwordTextField.anchor(top: usernameTextField.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 8, paddingLeft: 25, paddingBottom: 0, paddingRight: 25, width: 0, height: 40)
signUpButton.anchor(top: passwordTextField.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 8, paddingLeft: 25, paddingBottom: 0, paddingRight: 25, width: 0, height: 40)
if #available(iOS 11.0, *) {
noAccountButton.anchor(top: nil, left: view.leftAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 50)
} else {
// Fallback on earlier versions
noAccountButton.anchor(top: nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 50)
}
}
@objc func handleInputChange() {
if emailTextField.text?.count ?? 0 > 0 && passwordTextField.text?.count ?? 0 > 0, usernameTextField.text?.count ?? 0 > 0 {
self.signUpButton.backgroundColor = ColorCodes.instagramBlue
self.signUpButton.isEnabled = true
} else {
self.signUpButton.isEnabled = false
self.signUpButton.backgroundColor = UIColor.lightGray
}
}
@objc func handleShowSignUp() {
UIApplication.shared.statusBarStyle = .lightContent
self.navigationController?.popViewController(animated: true)
}
@objc func handleSignup() {
guard let email = emailTextField.text, let username = usernameTextField.text, let password = passwordTextField.text else { return }
signUpButton.setTitle("Loading...", for: .normal)
signUpButton.isEnabled = false
signUpButton.backgroundColor = UIColor.lightGray
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if let error = error {
print("failed to authenticate user", error)
self.signUpButton.setTitle("Sign Up", for: .normal)
self.signUpButton.backgroundColor = ColorCodes.instagramBlue
self.signUpButton.isEnabled = true
} else {
guard let image = self.plusImageView.image else { return }
guard let uploadData = UIImageJPEGRepresentation(image, 0.2) else { return }
let filename = UUID().uuidString
let storageRef = Storage.storage().reference().child("profile_images").child(filename)
storageRef.putData(uploadData, metadata: nil) { (metadata, err) in
if let err = err {
print("failed to upload image", err)
}
guard let userId = user?.user.uid else { return }
guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return } (HIER MELDET DEN FEHLER : Value of type 'StorageMetadata' has no member 'downloadURL')
let values: [String: Any] = ["username": username, "email": email, "profileImageUrl": profileImageUrl]
let userRef = Database.database().reference().child("users").child(userId)
userRef.updateChildValues(values, withCompletionBlock: { (err, _) in
if let err = err {
print("failed to upload user", err)
}
guard let mainTabBarController = UIApplication.shared.keyWindow?.rootViewController as? MainTabBarController else { return }
mainTabBarController.setupViewControllers()
self.dismiss(animated: true, completion: nil)
})
}
}
}
}
}