Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enter code here
- @IBOutlet weak var profileImageView: UIImageView!
- @IBOutlet weak var tapToChangeProfileButton: UIButton!
- var continueButton:RoundedWhiteButton!
- var imagePicker:UIImagePickerController!
- override func viewDidLoad() {
- super.viewDidLoad()
- continueButton.addTarget(self, action: #selector(handleSignUp), for:
- .touchUpInside)
- let imageTap = UITapGestureRecognizer(target: self, action:
- #selector(openImagePicker))
- profileImageView.isUserInteractionEnabled = true
- profileImageView.addGestureRecognizer(imageTap)
- profileImageView.layer.cornerRadius = profileImageView.bounds.height / 2
- profileImageView.clipsToBounds = true
- imagePicker = UIImagePickerController()
- imagePicker.allowsEditing = true
- imagePicker.sourceType = .photoLibrary
- imagePicker.delegate = self
- }
- @objc func openImagePicker(_ sender:Any) {
- self.present(imagePicker, animated: true, completion: nil)
- }
- @objc func handleSignUp() {
- guard let image = profileImageView.image else { return }
- Auth.auth().createUser(withEmail: email, password: pass) { user, error in
- if error == nil && user != nil {
- print("User created!")
- // 1. Upload the profile image to Firebase Storage
- self.uploadProfileImage(image) { url in
- if url != nil {
- let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
- changeRequest?.displayName = username
- changeRequest?.photoURL = url
- changeRequest?.commitChanges { error in
- if error == nil {
- print("User display name changed!")
- self.saveProfile(username: username, profileImageURL: url!) { success in
- if success {
- self.dismiss(animated: true, completion: nil)
- } else {
- self.resetForm()
- }
- }
- } else {
- print("Error: (error!.localizedDescription)")
- self.resetForm()
- }
- }
- } else {
- self.resetForm()
- }
- }
- } else {
- self.resetForm()
- }
- }
- }
- func resetForm() {
- let alert = UIAlertController(title: "Error signing up", message: nil, preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
- self.present(alert, animated: true, completion: nil)
- setContinueButton(enabled: true)
- continueButton.setTitle("Continue", for: .normal)
- activityView.stopAnimating()
- }
- func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
- guard let uid = Auth.auth().currentUser?.uid else { return }
- let storageRef = Storage.storage().reference().child("user/(uid)")
- guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }
- let metaData = StorageMetadata()
- metaData.contentType = "image/jpg"
- storageRef.putData(imageData, metadata: metaData) { metaData, error in
- if error == nil, metaData != nil {
- if let url = metaData?.downloadURL() {
- completion(url)
- } else {
- completion(nil)
- }
- // success!
- } else {
- // failed
- completion(nil)
- }
- }
- }
- func saveProfile(username:String, profileImageURL:URL, completion: @escaping ((_ success:Bool)->())) {
- guard let uid = Auth.auth().currentUser?.uid else { return }
- let databaseRef = Database.database().reference().child("users/profile/(uid)")
- let userObject = [
- "username": username,
- "photoURL": profileImageURL.absoluteString
- ] as [String:Any]
- databaseRef.setValue(userObject) { error, ref in
- completion(error == nil)
- }
- }
- func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
- picker.dismiss(animated: true, completion: nil)
- }
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
- if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
- self.profileImageView.image = pickedImage
- }
- picker.dismiss(animated: true, completion: nil)
- }
Add Comment
Please, Sign In to add comment