iOS Development
ios – How to upload a post

I am trying to upload a post with just textwords without images attached I need the text to be a separate text but my code only uploads the post that has a image attached to it and the post with just words don’t show up in firebase
How do I fix this code to allow my user upload their post without images to firebase
This is the uploadPostViewModel I have the text as a string but yet the data is not being uploaded as if it not listed but the image is what am I missing from this code that’s not allowing my text data to be uploaded to firebase
class UploadPostViewModel: ObservableObject {
@Published var didUploadPost = false
@Published var error: Error?
@Published var profileImage: Image?
@Published var caption = ""
@Published var text = ""
@Published var selectedImage: PhotosPickerItem? {
didSet { Task { await loadImage(fromItem: selectedImage) } }
}
private var uiImage: UIImage?
func uploadPost() async throws {
guard let uid = Auth.auth().currentUser?.uid else { return }
guard let image = uiImage else { return }
do {
guard let image = try await ImageUploader.uploadImage(image: image, type: .post) else { return }
let post = Post(ownerUid: uid,text: text, caption: caption, likes: 0, replyCount: 67, image: image, timestamp: Timestamp())
try await PostService.uploadPost(post)
self.didUploadPost = true
} catch {
print("DEBUG: Failed to upload image with error \(error.localizedDescription)")
self.error = error
}
}
func loadImage(fromItem item: PhotosPickerItem?) async {
guard let item = item else { return }
guard let data = try? await item.loadTransferable(type: Data.self) else { return }
guard let uiImage = UIImage(data: data) else { return }
self.uiImage = uiImage
self.profileImage = Image(uiImage: uiImage)
}
}
@State var captionText = ""
@StateObject var viewModel = UploadPostViewModel()
@Environment(\.dismiss) var dismiss
@Binding var tabIndex: Int
private var user: Userss? {
return ThirdUserService.shared.currentUser
}
var body: some View {
NavigationStack {
VStack {
HStack(alignment: .top) {
RectangularImageSize(user: user, size: .small)
VStack(alignment: .leading, spacing: 4) {
Text(user?.username ?? "")
.fontWeight(.semibold)
TextField("Type Something", text: $viewModel.text, axis: .vertical)
}
.font(.footnote)
Spacer()
if !viewModel.text.isEmpty {
Button {
viewModel.text = ""
} label: {
Image(systemName: "xmark")
.resizable()
.frame(width: 12, height: 12)
.foregroundColor(.gray)
}
}
}
Spacer()
}
.padding()
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
dismiss()
}
.font(.subheadline)
.foregroundStyle(Color.black)
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("Post") {
Task {
try await viewModel.uploadPost()
dismiss()
}
}
.opacity(viewModel.text.isEmpty ? 0.5 : 1.0)
.disabled(viewModel.text.isEmpty)
.font(.subheadline)
.fontWeight(.semibold)
.foregroundStyle(Color.green)
}
}
.onDisappear { tabIndex = 0 }
}
}
}
The post ios – How to upload a post appeared first on Ismmailgsm.
Credit: www.ismmailgsm.com