HINWEIS: Dies ist kein Duplikat, da ich in swift arbeite, nicht in Objective C, also bitte nicht als solches markieren.Wie finden Sie, wie viele Bytes in Swift NSURLSession lesen?
ich den folgenden Code eine Datei herunterzuladen:
func loadFileAsync(url: NSURL, name: String, completion:(path:String, error:NSError!) -> Void) {
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL!
let destinationUrl = documentsUrl.URLByAppendingPathComponent(name)
tempDocPath = destinationUrl.path!
if NSFileManager().fileExistsAtPath(destinationUrl.path!) {
print("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 {
print("response=\(response)")
if response.statusCode == 200 {
if data!.writeToURL(destinationUrl, atomically: true) {
print("file saved [\(destinationUrl.path!)]")
completion(path: destinationUrl.path!, error:error)
} else {
print("error saving file")
let error = NSError(domain:"Error saving file", code:1001, userInfo:nil)
completion(path: destinationUrl.path!, error:error)
}
}
}
}
else {
print("Failure: \(error!.localizedDescription)");
completion(path: destinationUrl.path!, error:error)
}
})
task.resume()
}
}
Was muß ich das Bytes zu finden, lesen und Bytes zu lesen, um zu erwarten eine Fortschrittsanzeige angezeigt werden?
NSURLSession verfügt über Delegatprotokolle für den Zugriff auf den Fortschritt. – Putz1103
@ Putz1103 Ich brauche die Antwort in swift- das ist Objective C –
Konvertieren zwischen den beiden ist keine lächerlich schwere Aufgabe, aber ich werde es für Sie suchen. – Putz1103