Skip to content

Commit

Permalink
add the destination to move the downloaded file
Browse files Browse the repository at this point in the history
  • Loading branch information
nghialv committed Mar 27, 2015
1 parent 3d3b61e commit d073c08
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
10 changes: 6 additions & 4 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ class ViewController: UIViewController {
let downloadUrl2 = "https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/iphoneappprogrammingguide.pdf"
let downloadUrl3 = "https://s3.amazonaws.com/hayageek/downloads/SimpleBackgroundFetch.zip"
let uploadUrl = "http://httpbin.org/post"

let documentsPath: AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
let destination = NSURL(string: documentsPath.stringByAppendingString("/file.pdf"))!

// Downloading tasks
let task1 = DownloadTask(url: downloadUrl1)
let task1 = DownloadTask(url: downloadUrl1, destination: destination)
.progress { cur, total in
let per = Double(cur) / Double(total)
println("task1: downloading: \(per)")
Expand All @@ -31,7 +33,7 @@ class ViewController: UIViewController {
println("task1: completed")
}

let task2 = DownloadTask(url: downloadUrl2)
let task2 = DownloadTask(url: downloadUrl2, destination: destination)
.progress { cur, total in
let per = Double(cur) / Double(total)
println("task2: downloading: \(per)")
Expand All @@ -40,7 +42,7 @@ class ViewController: UIViewController {
println("task2: completed")
}

let task3 = DownloadTask(url: downloadUrl3)
let task3 = DownloadTask(url: downloadUrl3, destination: destination)
.progress { cur, total in
let per = Double(cur) / Double(total)
println("task3: downloading: \(per)")
Expand Down
5 changes: 4 additions & 1 deletion Transporter/DownloadTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import Foundation

public class DownloadTask : TPTransferTask {
var task: NSURLSessionDownloadTask?
var destination: NSURL
var movingError: NSError?

override init(url: String, params: [String: AnyObject]? = nil) {
init(url: String, destination: NSURL, params: [String: AnyObject]? = nil) {
self.destination = destination
super.init(url: url, params: params)
method = .GET
}
Expand Down
3 changes: 3 additions & 0 deletions Transporter/TPTaskGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,8 @@ extension TPTaskGroup : NSURLSessionDownloadDelegate {
// Download task completes successfully
public func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
NSLog("[Session] Download finished : \(location)")
if let task = sessionTasks[downloadTask] as? DownloadTask {
NSFileManager.defaultManager().moveItemAtURL(location, toURL: task.destination, error: &task.movingError)
}
}
}

0 comments on commit d073c08

Please sign in to comment.