0

I have this below method do download a pdf file and save it to documents directory. But the file is not showing up in Files app. I am getting success in logs for creating directory and for writing the file as well. I need help to save the file to Files app without using UIActivityViewController.

I wrote this code after reading some answers in SO and some other tutorials.

@objc
func downloadButtonPushed(sender: AnyObject) -> Void {
    //
    Logger.shared.log(.web, .debug, "downloading pdf at url = \(String(describing: self.url))")

    let hud  = MBProgressHUD.showAdded(to: self.view, animated: true)
    hud.isUserInteractionEnabled = true

    Alamofire.request(self.url).responseData { (responseData) in

        guard let data = responseData.data else {
            Logger.shared.log(.web, .debug, "error = \(responseData)")
            MBProgressHUD.hide(for: self.view, animated: true)
            return
        }

        let fileManager = FileManager.default
        do {
            let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
            var fileURL = documentDirectory.appendingPathComponent("/Haraj")

            do {
                try fileManager.createDirectory(at: fileURL, withIntermediateDirectories: true, attributes: nil)
                Logger.shared.log(.web, .debug, "Directory created")
            }
            catch {
                Logger.shared.log(.web, .debug, "Directory creating failed")
            }

            fileURL.appendPathComponent("/report.pdf")

            do {
                try data.write(to: fileURL)
                 Logger.shared.log(.web, .debug, "File written success")
            }
            catch {
                Logger.shared.log(.web, .debug, "File writing failed")
            }
        } catch {
            Logger.shared.log(.web, .debug, "File writing failed")
        }


        MBProgressHUD.hide(for: self.view, animated: true)
    }
}
Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65
  • There is no need to add a slash prefix to your path component `appendingPathComponent("Haraj", isDirectory: true)` and `appendPathComponent("report.pdf")` – Leo Dabus Nov 14 '19 at 15:52
  • Files App related https://stackoverflow.com/a/46457518/2303865 – Leo Dabus Nov 14 '19 at 15:55

1 Answers1

2

The documents directory you are saving to..

let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)

is a Documents directory within your application on the device, nothing to do with the files app.

App's are sandboxed and have their own folder structure inside of the app. The Files App is another app and also has its own folder structure and can access some other online storage providers too.

If you want to store your file in the Files app, I think you have to use either UIDocumentInteractionController or UIActivityViewController to share the content with the Files app.

You cannot access the Files app folders directly.

Scriptable
  • 19,402
  • 5
  • 56
  • 72