https://console.firebase.google.com/
ContentView.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | // // ContentView.swift // DevSwiftUI // // Created by Cairocoders // import SwiftUI import Firebase import FirebaseStorage struct ContentView: View { @State var email = "" @State var password = "" @State var StatusMessage = "" @State var shouldShowImagePicker = false @State var image: UIImage? var body: some View { NavigationView { ScrollView { VStack(spacing: 16) { Text( "Create Account" ) Button { shouldShowImagePicker.toggle() } label: { VStack { if let image = self.image { Image(uiImage: image) .resizable() .scaledToFill() .frame(width: 128, height: 128) .cornerRadius(64) } else { Image(systemName: "person.fill" ) .font(. system (size: 64)) .padding() .foregroundColor(Color(.label)) } } .overlay(RoundedRectangle(cornerRadius: 64) .stroke(Color.black, lineWidth: 3) ) } Group { TextField( "Email" , text: $email) .keyboardType(.emailAddress) .autocapitalization(.none) SecureField( "Password" , text: $password) } .padding(12) .background(Color.white) .cornerRadius(10) Button { handleAction() } label: { HStack { Spacer() Text( "Create Account" ) .foregroundColor(.white) .padding(.vertical, 10) .font(. system (size: 14, weight: .semibold)) Spacer() }.background(Color.blue) }.cornerRadius(10) Text(self.StatusMessage) } .padding() } .navigationViewStyle(StackNavigationViewStyle()) .fullScreenCover(isPresented: $shouldShowImagePicker, onDismiss: nil) { ImagePicker(image: $image) .ignoresSafeArea() } .background( LinearGradient(gradient: Gradient(colors: [.orange, .gray]), startPoint: .top, endPoint: .bottom) .edgesIgnoringSafeArea(.all)) } } private func handleAction() { createNewAccount() } private func createNewAccount() { Auth.auth().createUser(withEmail: email, password: password) { result, err in if let err = err { print( "Failed to create user:" , err) self.StatusMessage = "Failed to create user: \(err)" return } print( "Successfully created user: \(result?.user.uid ?? " ")" ) self.StatusMessage = "Successfully created user: \(result?.user.uid ?? " ")" self.persistImageToStorage() } } private func persistImageToStorage() { guard let uid = Auth.auth().currentUser?.uid else { return } let ref = Storage.storage().reference(withPath: uid) guard let imageData = self.image?.jpegData(compressionQuality: 0.5) else { return } ref.putData(imageData, metadata: nil) { metadata, err in if let err = err { self.StatusMessage = "Failed to push image to Storage: \(err)" return } ref.downloadURL { url, err in if let err = err { self.StatusMessage = "Failed to retrieve downloadURL: \(err)" return } self.StatusMessage = "Successfully stored image with url: \(url?.absoluteString ?? " ")" print(url?.absoluteString) } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // // ImagePicker.swift // DevSwiftUI // // Created by Cairocoders // import SwiftUI struct ImagePicker: UIViewControllerRepresentable { @Binding var image: UIImage? private let controller = UIImagePickerController() func makeCoordinator() -> Coordinator { return Coordinator(parent: self) } class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { let parent: ImagePicker init(parent: ImagePicker) { self.parent = parent } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { parent.image = info[.originalImage] as? UIImage picker.dismiss(animated: true ) } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.dismiss(animated: true ) } } func makeUIViewController(context: Context) -> some UIViewController { controller.delegate = context.coordinator return controller } func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // // DevSwiftUIApp.swift // DevSwiftUI // // Created by Cairocoders // import SwiftUI import Firebase @main struct DevSwiftUIApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate var body: some Scene { WindowGroup { ContentView() } } } class AppDelegate: NSObject,UIApplicationDelegate{ func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { FirebaseApp.configure() return true } } |