Guest User

Untitled

a guest
Jul 25th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. enter code here
  2.  
  3. @IBOutlet weak var profileImageView: UIImageView!
  4. @IBOutlet weak var tapToChangeProfileButton: UIButton!
  5.  
  6. var continueButton:RoundedWhiteButton!
  7. var imagePicker:UIImagePickerController!
  8.  
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11.  
  12. continueButton.addTarget(self, action: #selector(handleSignUp), for:
  13. .touchUpInside)
  14. let imageTap = UITapGestureRecognizer(target: self, action:
  15. #selector(openImagePicker))
  16. profileImageView.isUserInteractionEnabled = true
  17. profileImageView.addGestureRecognizer(imageTap)
  18. profileImageView.layer.cornerRadius = profileImageView.bounds.height / 2
  19. profileImageView.clipsToBounds = true
  20.  
  21. imagePicker = UIImagePickerController()
  22. imagePicker.allowsEditing = true
  23. imagePicker.sourceType = .photoLibrary
  24. imagePicker.delegate = self
  25. }
  26.  
  27. @objc func openImagePicker(_ sender:Any) {
  28. self.present(imagePicker, animated: true, completion: nil)
  29. }
  30.  
  31. @objc func handleSignUp() {
  32. guard let image = profileImageView.image else { return }
  33.  
  34.  
  35. Auth.auth().createUser(withEmail: email, password: pass) { user, error in
  36. if error == nil && user != nil {
  37. print("User created!")
  38.  
  39.  
  40. // 1. Upload the profile image to Firebase Storage
  41.  
  42. self.uploadProfileImage(image) { url in
  43.  
  44. if url != nil {
  45. let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
  46. changeRequest?.displayName = username
  47. changeRequest?.photoURL = url
  48.  
  49. changeRequest?.commitChanges { error in
  50. if error == nil {
  51. print("User display name changed!")
  52.  
  53. self.saveProfile(username: username, profileImageURL: url!) { success in
  54. if success {
  55. self.dismiss(animated: true, completion: nil)
  56. } else {
  57. self.resetForm()
  58. }
  59. }
  60.  
  61. } else {
  62. print("Error: (error!.localizedDescription)")
  63. self.resetForm()
  64. }
  65. }
  66. } else {
  67. self.resetForm()
  68. }
  69.  
  70. }
  71.  
  72. } else {
  73. self.resetForm()
  74. }
  75. }
  76. }
  77.  
  78. func resetForm() {
  79. let alert = UIAlertController(title: "Error signing up", message: nil, preferredStyle: .alert)
  80. alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
  81. self.present(alert, animated: true, completion: nil)
  82.  
  83. setContinueButton(enabled: true)
  84. continueButton.setTitle("Continue", for: .normal)
  85. activityView.stopAnimating()
  86. }
  87.  
  88. func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
  89. guard let uid = Auth.auth().currentUser?.uid else { return }
  90. let storageRef = Storage.storage().reference().child("user/(uid)")
  91.  
  92. guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }
  93.  
  94. let metaData = StorageMetadata()
  95. metaData.contentType = "image/jpg"
  96.  
  97. storageRef.putData(imageData, metadata: metaData) { metaData, error in
  98. if error == nil, metaData != nil {
  99. if let url = metaData?.downloadURL() {
  100. completion(url)
  101. } else {
  102. completion(nil)
  103. }
  104. // success!
  105. } else {
  106. // failed
  107. completion(nil)
  108. }
  109. }
  110. }
  111.  
  112. func saveProfile(username:String, profileImageURL:URL, completion: @escaping ((_ success:Bool)->())) {
  113. guard let uid = Auth.auth().currentUser?.uid else { return }
  114. let databaseRef = Database.database().reference().child("users/profile/(uid)")
  115.  
  116. let userObject = [
  117. "username": username,
  118. "photoURL": profileImageURL.absoluteString
  119. ] as [String:Any]
  120.  
  121. databaseRef.setValue(userObject) { error, ref in
  122. completion(error == nil)
  123. }
  124. }
  125.  
  126. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  127. picker.dismiss(animated: true, completion: nil)
  128. }
  129.  
  130.  
  131. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  132.  
  133. if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
  134. self.profileImageView.image = pickedImage
  135. }
  136.  
  137. picker.dismiss(animated: true, completion: nil)
  138. }
Add Comment
Please, Sign In to add comment