iOS Development

ios – Swift importing file to Firebase Storage not working, unsuitable file permissions

Spread the love


I am making a easy messaging app the place customers can ship information to one another. I will probably be storing the information in Firestore storage. So I must add them from a customers native machine. When I attempt to add a file I get the error under.

UserInfo={NSLocalizedDescription=File at URL: file:///non-public/var/cell/Containers/Shared/AppGroup/65792EE7-9583-4D3F-9A8B-249DEAA19303/Filepercent20Providerpercent20Storage/IMG_6187.PNG is just not reachable. Guarantee file URL is just not a listing, symbolic hyperlink, or invalid url.}

I efficiently open the file, and I may even learn it. I believe the url being handed to the uploadFile() func is unsuitable.

import SwiftUI
import UniformTypeIdentifiers
import Firebase
import UIKit
import FirebaseStorage

struct FilePicker: View {
    @State non-public var showPicker = false
    @State non-public var selectedURL: URL?

    var physique: some View {
        VStack {
            Spacer()
            if let url = selectedURL {
                Button(motion: {
                    ImageUploader.uploadFile(url: url, location: "information") { loc in
                        print("loc is(loc)")
                    }
                }, label: {
                    Textual content("add")
                })
            }
            Button(motion: {
                showPicker.toggle()
            }, label: {
                Picture(systemName: "paperclip")
                    .padding()
                    .foregroundStyle(.blue)
                    .background(.grey)
                    .clipShape(Circle())
            })
            Spacer()
        }
        .fileImporter(isPresented: $showPicker, allowedContentTypes: [UTType.data]) { lead to
            do {
                let url = strive end result.get()
                let accessing = url.startAccessingSecurityScopedResource()
                defer {
                    if accessing {
                        url.stopAccessingSecurityScopedResource()
                    }
                }
                selectedURL = url
            } catch {
                print("File picker error: (error)")
            }
        }
    }
    func uploadFile(url: URL, location: String, completion: @escaping(String) -> Void){
        let ref = Storage.storage().reference(withPath: "/(location)/(NSUUID().uuidString)")

        ref.putFile(from: url, metadata: nil) { metadata, error in
            if let err = error {
                print(err)
            }
            ref.downloadURL { (url, error) in
                guard let downloadURL = url?.absoluteString else {
                    completion("")
                    return
                }
                completion(downloadURL)
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *