Firebase Auth/Storage/FireStore を使った新規登録の処理を作っています。
以下は新規登録(まずAuthで認証し、帰ってきたUserをFireStoreに登録、画像があるならばそのURLも一緒に保存する)処理です。
swift
1static func signUp(name: String, email: String, password: String, image: UIImage?, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) { 2 Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in 3 if error != nil { 4 onError(error) 5 return 6 } 7 guard let uid = user?.user.uid else { return } 8 var dict: [String: Any] = [ 9 "name": name, 10 "email": email 11 ] 12 // If Image is Set 13 if let image = image { 14 StorageService.storage(image: image, path: .icon, id: uid) { (imageUrl) in 15 dict["iconUrl"] = imageUrl 16 } 17 } 18 Firestore.firestore().collection("users").document(uid).setData(dict) { (error) in 19 if let error = error { 20 print(error) 21 return 22 } 23 } 24 onSuccess() 25 }) 26 }
以下はStorageのUIImageを引数にとり、URLを返す処理を関数にしたものです
swift
1class StorageService { 2 3 // Upload Image to Storage 4 static func storage(image: UIImage?, path: PathType, id: String, completion: @escaping (_ imageUrl: String?) -> ()) { 5 guard let image = image, let imageData = UIImageJPEGRepresentation(image, 0.1) else { 6 print("Non Image") 7 completion(nil) 8 return 9 } 10 let storageRef = Storage.storage().reference().child(path.rawValue).child(id) 11 storageRef.putData(imageData, metadata: nil, completion: { (metaData, error) in 12 if let error = error { 13 print("Fail to Put Data in Storage : (error)") 14 completion(nil) 15 return 16 } 17 storageRef.downloadURL { (imageUrl, error) in 18 if let error = error { 19 print("Fail to Download Url : (error)") 20 completion(nil) 21 return 22 } 23 if let imageUrl = imageUrl?.absoluteString { 24 completion(imageUrl) 25 } 26 } 27 }) 28 } 29} 30
Authの登録と、FireStoreへの保存は成功するのですが、画像がある時、
Storageには保存されているのに、FireStoreには画像のURlが保存されません。
storage()クロージャの書き方などに問題があるのでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。