2

アプリのダウンローダーを作成する必要があり、ダウンロードを一時停止、続行、キャンセルできます。また、ダウンロードを一時停止し、アプリを強制終了し、アプリを再度開いて一時停止した場所から続行することもサポートする必要があります。

ダウンロードしたデータの保存方法と継続方法を教えてください。

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

これは、アプリの実行中に機能します。ただし、アプリの終了時にデータを保存し、アプリの起動時にデータを続行する必要があります。ダウンロードしたデータを保存する方法と継続する方法がわかりません。どんな助けでも感謝します。

4

1 に答える 1

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

しかし、毎回「履歴書データが空でした」と表示されます。私は同じ目的地を与えます。しかし、履歴書データをキャッチすることはできません。Alamofire の例は見つかりませんでした。

于 2015-12-15T06:07:20.537 に答える