我需要为我的应用编写一个下载器,它可以暂停,继续和取消下载。此外,它必须支持暂停下载,终止应用程序,然后重新打开应用程序并从暂停的位置继续。
如何保存下载的数据以及如何继续下载?
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)
}
}
这在应用程序运行时有效。但我需要在应用程序终止时保存数据,并在应用程序启动时继续保存。我不知道如何保留下载的数据以及如何继续下载。任何帮助都会得到满足。
答案 0 :(得分:0)
我在Alamofire文档中找到了这个:
Alamofire.download(.GET, "https://httpbin.org/stream/100", destination: destination)
.response { _, _, data, _ in
if let
data = data,
resumeDataString = NSString(data: data, encoding: NSUTF8StringEncoding)
{
print("Resume Data: \(resumeDataString)")
} else {
print("Resume Data was empty")
}
}
但是我得到了#34;简历数据是空的"每次。我给同一个目的地。但它无法捕获简历数据。我无法找到Alamofire的例子。