NOTE: This is not a duplicate because I am working in swift, not Objective C, so please don't mark it as such.
I have the following code to download a file:
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()
}
}
What do I need to have to find the bytes read and bytes expected to read in order to display a progress view?