Search code examples
iosswiftfirebase-storagensfilemanager

Saved pdfFile not found in "files" directory on iphone


I looked for all the "save pdf file" questions on 'SO' and at this moment I banging my head against a wall with this issue:

I download a pdf file (from firebase storage) and try saving it with that code:

static func getPdf(firebaseStoragePath:String){
    let ref = Storage.storage().reference().child(firebaseStoragePath)

    let documentsURL:NSURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
    let fileURL = documentsURL.appendingPathComponent("doc.pdf")
    print("***fileURL: ",fileURL ?? "")

    let task = ref.write(toFile: fileURL!)

    task.observe(StorageTaskStatus.success) { (snap) in
        print("success")
    }
}

It seams that the download is successful but the file is not showing up under "files" folder on my iPhone and can not find way to access it. The file url that is printed out looks like this:

file:///var/mobile/Containers/Data/Application/635B2D57-CA7B-44EF-BDF1-4308CABD8ED5/Documents/doc.pdf

I tried to write it to ".downloadsDirectory"(as well as some others) but getting error that access is not permitted.

What am I doing wrong?


Solution

  • Here your pdf file is stored inside the document space of your application. So you can't see it in the Files app. Every documents saved inside the Document folder of your application can be seen via iTunes in the File sharing but you need to add the permission in the Info.plist first:

    • UIFileSharingEnabled: Application supports iTunes file sharing

    In order to store a document in Files you need to use the UIDocumentInteractionController library:

    let documentInteractionController = UIDocumentInteractionController()
    
    func downloadPdf(firebaseStoragePath:String){
        let ref = Storage.storage().reference().child(firebaseStoragePath)
    
        let documentsURL: NSURL = FileManager.default.urls(for: .temporaryDirectory, in: .userDomainMask).first! as NSURL
        let fileURL = documentsURL.appendingPathComponent("doc.pdf")
    
        let task = ref.write(toFile: fileURL!)
    
        task.observe(StorageTaskStatus.success) { (snap) in
            DispatchQueue.main.async {
                documentInteractionController.url = documentsURL
                documentInteractionController.uti = documentsURL.typeIdentifier ?? "public.data, public.content"
                documentInteractionController.name = documentsURL.localizedName ?? url.lastPathComponent
                documentInteractionController.presentPreview(animated: true)
            }
        }
    }
    
    extension URL {
        var typeIdentifier: String? {
            return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier
        }
        var localizedName: String? {
            return (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName
        }
    }
    

    Don't forget to add these permission in the Info.plist file:

    • UIFileSharingEnabled: Application supports iTunes file sharing
    • LSSupportsOpeningDocumentsInPlace: Supports opening documents in place

    For more information about the use of Files and UIDocumentInteractionController you can check this blog post.