我需要为我的应用程序编写一个下载器,它可以暂停、继续和取消下载。它还必须支持暂停下载、终止应用程序并重新打开应用程序并从暂停处继续。
我怎样才能保留下载的数据,我怎样才能继续呢?
import UIKit
import Foundation
import Alamofire
class DownloaderViewController: UIViewController {
@IBOutlet weak var label: UILabel!
let progressIndicatorView = UIProgressView()
var request: Alamofire.Request?
override func viewDidLoad() {
super.viewDidLoad()
}
}
@IBAction func cancelBtn(sender: AnyObject) {
self.request?.cancel()
self.label.text = "% 0.0"
}
@IBAction func pauseBtn(sender: AnyObject) {
self.request?.suspend()
}
@IBAction func continueBtn(sender: AnyObject) {
self.request?.resume()
}
@IBAction func startBtn(sender: AnyObject) {
var localPath: NSURL?
self.request = Alamofire.download(.GET, "https://dl.dropboxusercontent.com/u/11563257/3.%20Interactive_iPad_test_for_PDF_EXPERT.pdf", destination: { (temporaryURL, response) in
let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pathComponent = response.suggestedFilename
localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
return localPath!
}).progress() {
(_, totalBytesRead, totalBytesExpectedToRead) in
dispatch_async(dispatch_get_main_queue()) {
self.progressIndicatorView.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead), animated: true)
self.updateProgress(self.progressIndicatorView)
if totalBytesRead == totalBytesExpectedToRead {
self.progressIndicatorView.removeFromSuperview()
}
}
}
func updateProgress(prg:UIProgressView) {
let stepSize:Float = 0.1
prg.setProgress(prg.progress + stepSize, animated: true)
self.label.text = "% " + String(format: "%.2f", prg.progress*100)
}
}
这在应用程序运行时有效。但是我需要在应用程序终止时保存数据并在应用程序启动时继续它。我不知道如何保留下载的数据以及如何继续它。任何帮助都会得到帮助。