0

I am using a uIdocumentPickerViewController to upload a pdf file in firebase but when I try to upload it, I get an error saying that this file is not reachable.

Here is the code of the delegate of my uidocumentPickerViewController :

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

            let storageReference = Storage.storage().reference()
            print(urls.first!)




            storageReference.child((urls.first?.deletingPathExtension().lastPathComponent)!).putFile(from: urls.first!, metadata: nil){(_, err) in
                if err != nil {
                    print((err?.localizedDescription)!)
                    return
                }

                print("success")
            }


        }

Here is the mistake I get after choosing my pdf file :

File at URL: file:///private/var/mobile/Containers/Shared/AppGroup/7BE40C45-A4B5-4EE0-8AFF-789B2F93C1F6/File%20Provider%20Storage/Document%20PDF.pdf is not reachable.

Domain=NSCocoaErrorDomain Code=257 xcode

sofiane_dv
  • 71
  • 7

1 Answers1

0

I am using UIDocumentPicker like below and it works fine:

First I have an enum,

enum UploadFileType{
    case photo
    case file
}

Then a structure:

struct UploadFileData {
    var fileName: String
    var fileType: UploadFileType
    var fileData: NSData
}

var file: UploadFileData?

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    let fileManager = FileManager.default
    print(fileManager.fileExists(atPath: url.path))
    let data = NSData(contentsOfFile: url.path)
    guard let doc = data else {
        //  Utility.showAlert(message: Strings.ERROR.text, title: Strings.DOCUMENT_FORMAT_NOT_SUPPORTED.text, controller: self)
        return
    }
    self.file = UploadFileData(fileName: "\(url)", fileType: .file, fileData: doc)
    guard let dataFile = self.file?.fileData as Data? else {return}
}
Abdul Karim Khan
  • 4,256
  • 1
  • 26
  • 30