2016-07-21 9 views
0

Ich habe ein Beispiel gefunden, das zeigt, wie man die Synchronisierung & async unter stackoverflow.com here! durchführt Dieser Code ist einfach perfekt! Ich möchte wissen, wie Sie Fortschrittsbalken für den Download-Fortschritt zur Anzeige hinzufügen können. Was ist der einfachste und schnellste Ansatz dafür? Ich habe eine Tabellenansicht mit einigen Dateien zum Download und jede Zelle height ist mehr als 200px, so wäre es schön, wenn ich eine Ansicht oder Unteransicht programmgesteuert hinzufügen und entlassen kann, wenn der Download abgeschlossen ist.Fortschrittsbalken Download Statusanzeige Swift iOS

Swift Code veröffentlicht von djunod

import Foundation 

class HttpDownloader { 

    class func loadFileSync(url: NSURL, completion:(path:String, error:NSError!) -> Void) { 
     let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as! NSURL 
     let destinationUrl = documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!) 
     if NSFileManager().fileExistsAtPath(destinationUrl.path!) { 
      println("file already exists [\(destinationUrl.path!)]") 
      completion(path: destinationUrl.path!, error:nil) 
     } else if let dataFromURL = NSData(contentsOfURL: url){ 
      if dataFromURL.writeToURL(destinationUrl, atomically: true) { 
       println("file saved [\(destinationUrl.path!)]") 
       completion(path: destinationUrl.path!, error:nil) 
      } else { 
       println("error saving file") 
       let error = NSError(domain:"Error saving file", code:1001, userInfo:nil) 
       completion(path: destinationUrl.path!, error:error) 
      } 
     } else { 
      let error = NSError(domain:"Error downloading file", code:1002, userInfo:nil) 
      completion(path: destinationUrl.path!, error:error) 
     } 
    } 

    class func loadFileAsync(url: NSURL, completion:(path:String, error:NSError!) -> Void) { 
     let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as! NSURL 
     let destinationUrl = documentsUrl.URLByAppendingPathComponent(url.lastPathComponent!) 
     if NSFileManager().fileExistsAtPath(destinationUrl.path!) { 
      println("file already exists [\(destinationUrl.path!)]") 
      completion(path: destinationUrl.path!, error:nil) 
     } else { 
      let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration() 
      let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil) 
      let request = NSMutableURLRequest(URL: url) 
      request.HTTPMethod = "GET" 
      let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in 
       if (error == nil) { 
        if let response = response as? NSHTTPURLResponse { 
         println("response=\(response)") 
         if response.statusCode == 200 { 
          if data.writeToURL(destinationUrl, atomically: true) { 
           println("file saved [\(destinationUrl.path!)]") 
           completion(path: destinationUrl.path!, error:error) 
          } else { 
           println("error saving file") 
           let error = NSError(domain:"Error saving file", code:1001, userInfo:nil) 
           completion(path: destinationUrl.path!, error:error) 
          } 
         } 
        } 
       } 
       else { 
        println("Failure: \(error.localizedDescription)"); 
        completion(path: destinationUrl.path!, error:error) 
       } 
      }) 
      task.resume() 
     } 
    } 
} 

USAGE

let url = NSURL(string: "http://www.mywebsite.com/myfile.pdf") 
HttpDownloader.loadFileAsync(url, completion:{(path:String, error:NSError!) in 
       println("pdf downloaded to: \(path)") 
      }) 

Und wo soll ich bekommen, dass Werte von 0,0 bis 1,0? Ich verstehe, dass ich Gesamtgröße auf aktuelle heruntergeladene Größe teilen sollte aber wo sind diese Variablen?

Antwort

0

This answer provides information wie Sie von einer NSURLSession DataTask profitieren.

Sobald Sie diese Delegate Protokolle implementieren, können Sie den Fortschritt erhalten und tun, was auch immer Sie damit möchten. Aber stellen Sie sicher, dass Sie es im Hauptthread (dispatch_async zum Hauptthread) tun, um Ihre UI-Aktualisierungen durchzuführen.