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)
}
}