I am implementing a ViewController to display a PDF previously downloaded from my server and stored locally on the device, it works correctly, but to download the PDF takes too much time and I would like to implement a progress-bar.
My code is the following, where I have tried to implement the @IBOutlet weak var downloadBar: UIProgressView!
.
As I get the time it takes for the download, so I eat my code reaches 100% and the download does not end yet.
class PDFViewController: UIViewController {
@IBOutlet weak var pdfView: PDFView!
@IBOutlet weak var downloadBar: UIProgressView!
//*******
var downloader = Timer()
var minValue = 0
var maxValue = 100
//********
var namePDF:String?
override func viewDidLoad() {
super.viewDidLoad()
downloadBar.setProgress(0, animated: false)
if let pdfUrl = URL(string: "https://miserver.com/\(namePDF!).pdf") {
print(pdfUrl)
// then lets create your document folder url
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// lets create your destination file url
let destinationUrl = documentsDirectoryURL.appendingPathComponent(pdfUrl.lastPathComponent)
print(destinationUrl)
// to check if it exists before downloading it
if FileManager.default.fileExists(atPath: destinationUrl.path) {
print("The file already exists at path")
/************** show pdf ****************/
let pdfUrl = destinationUrl.path
let rutafile = URL(fileURLWithPath: pdfUrl)
print(pdfUrl)
if let document = PDFDocument(url: rutafile) {
pdfView.autoScales = true
pdfView.document = document
}
/************** end show pdf ****************/
// if the file doesn't exist
} else {
print("file doesn't exist")
downloader = Timer.scheduledTimer(timeInterval: 0.06, target: self, selector: (#selector(PDFViewController.updater)), userInfo: nil, repeats: true)
downloadBar.setProgress(0, animated: false)
// you can use NSURLSession.sharedSession to download the data asynchronously
URLSession.shared.downloadTask(with: pdfUrl, completionHandler: { (location, response, error) -> Void in
guard let location = location, error == nil else { return }
do {
// after downloading your file you need to move it to your destination url
try FileManager.default.moveItem(at: location, to: destinationUrl)
print("File moved to documents folder")
print("file has already been downloaded")
/************** show pdf ****************/
let pdfUrl = destinationUrl.path
let rutafile = URL(fileURLWithPath: pdfUrl)
print(pdfUrl)
if let document = PDFDocument(url: rutafile) {
self.pdfView.autoScales = true
self.pdfView.document = document
}
/************** show pdf ****************/
} catch let error as NSError {
print(error.localizedDescription)
}
}).resume()
}
}
}
@objc func updater() {
if minValue != maxValue {
minValue += 1
downloadBar.progress = Float(minValue) / Float(maxValue)
print(Float(minValue) / Float(maxValue))
} else {
minValue = 0
downloader.invalidate()
}
}
}
From already thank you very much