アプリのダウンローダーを作成する必要があり、ダウンロードを一時停止、続行、キャンセルできます。また、ダウンロードを一時停止し、アプリを強制終了し、アプリを再度開いて一時停止した場所から続行することもサポートする必要があります。
ダウンロードしたデータの保存方法と継続方法を教えてください。
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)
}
}
これは、アプリの実行中に機能します。ただし、アプリの終了時にデータを保存し、アプリの起動時にデータを続行する必要があります。ダウンロードしたデータを保存する方法と継続する方法がわかりません。どんな助けでも感謝します。