Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Swift 2.0, Update Sample Project, Fix Crashes #16

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TCBlobDownloadSwift

Powerful file downloads for iOS 7+ with [NSURLSession][nsurlsession-url] in Swift.
Powerful file downloads for iOS 8+ with [NSURLSession][nsurlsession-url] in Swift.

TCBlobDownloadSwift makes it easy to quickly download one or several large file(s) from your backend or the Internet right into your app. Give it an URL, a directory (or not, it can also download into the user's tmp folder), a name (or not, it can also give your file a default name), and it will take care of everything.

Expand All @@ -17,7 +17,7 @@ See the [Usage](#usage) section for examples.

### Requirements

- iOS 7.0+ / Mac OS X 10.9+
- iOS 8.0+ / Mac OS X 10.9+

### Installation

Expand Down
2 changes: 1 addition & 1 deletion Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.thibaultcha.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
50 changes: 31 additions & 19 deletions Source/TCBlobDownload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ public class TCBlobDownload {
/// Will contain an error if the downloaded file couldn't be moved to its final destination.
var error: NSError?

/// Current progress of the download, a value between 0 and 1. 0 means nothing was received and 1 means the download is completed.
/// Current progress of the download, a value between 0 and 1. 0 means nothing was received and 1 means the download is completed. If expected size if unknown value equals -1
public var progress: Float = 0

/// Current progress of the download, bytes of downloaded part
public var totalBytesWritten: Int64 = 0

/// Current progress of the download, whole size in bytes. If expected size if unknown value equals -1
public var totalBytesExpectedToWrite: Int64 = 0

/// If the moving of the file after downloading was successful, will contain the `NSURL` pointing to the final file.
public var resultingURL: NSURL?
Expand All @@ -47,17 +53,16 @@ public class TCBlobDownload {
/// A computed destination URL depending on the `destinationPath`, `fileName`, and `suggestedFileName` from the underlying `NSURLResponse`.
public var destinationURL: NSURL {
let destinationPath = self.directory ?? NSURL(fileURLWithPath: NSTemporaryDirectory())

return NSURL(string: self.fileName!, relativeToURL: destinationPath!)!.URLByStandardizingPath!
return destinationPath.URLByAppendingPathComponent(self.fileName!)
}

/**
Initialize a new download assuming the `NSURLSessionDownloadTask` was already created.

:param: downloadTask The underlying download task for this download.
:param: directory The directory where to move the downloaded file once completed.
:param: fileName The preferred file name once the download is completed.
:param: delegate An optional delegate for this download.
- parameter downloadTask: The underlying download task for this download.
- parameter directory: The directory where to move the downloaded file once completed.
- parameter fileName: The preferred file name once the download is completed.
- parameter delegate: An optional delegate for this download.
*/
init(downloadTask: NSURLSessionDownloadTask, toDirectory directory: NSURL?, fileName: String?, delegate: TCBlobDownloadDelegate?) {
self.downloadTask = downloadTask
Expand Down Expand Up @@ -102,16 +107,23 @@ public class TCBlobDownload {
public func resume() {
self.downloadTask.resume()
}

/**
Convinience method
*/
public var isSuspended: Bool {
return self.downloadTask.state == .Suspended
}

/**
Cancel a download and produce resume data. If stored, this data can allow resuming the download at its previous state.

:see: `TCBlobDownloadManager -downloadFileWithResumeData`
:see: `NSURLSessionDownloadTask -cancelByProducingResumeData`

:param: completionHandler A completion handler that is called when the download has been successfully canceled. If the download is resumable, the completion handler is provided with a resumeData object.
- parameter completionHandler: A completion handler that is called when the download has been successfully canceled. If the download is resumable, the completion handler is provided with a resumeData object.
*/
public func cancelWithResumeData(completionHandler: (NSData!) -> Void) {
public func cancelWithResumeData(completionHandler: (NSData?) -> Void) {
self.downloadTask.cancelByProducingResumeData(completionHandler)
}

Expand All @@ -125,10 +137,10 @@ public protocol TCBlobDownloadDelegate: class {

:see: `NSURLSession -URLSession:dataTask:didReceiveData:`

:param: download The download that received a chunk of data.
:param: progress The current progress of the download, between 0 and 1. 0 means nothing was received and 1 means the download is completed.
:param: totalBytesWritten The total number of bytes the download has currently written to the disk.
:param: totalBytesExpectedToWrite The total number of bytes the download will write to the disk once completed.
- parameter download: The download that received a chunk of data.
- parameter progress: The current progress of the download, between 0 and 1. 0 means nothing was received and 1 means the download is completed.
- parameter totalBytesWritten: The total number of bytes the download has currently written to the disk.
- parameter totalBytesExpectedToWrite: The total number of bytes the download will write to the disk once completed.
*/
func download(download: TCBlobDownload, didProgress progress: Float, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)

Expand All @@ -137,16 +149,16 @@ public protocol TCBlobDownloadDelegate: class {

:see: `NSURLSession -URLSession:task:didCompleteWithError:`

:param: download The download that received a chunk of data.
:param: error An eventual error. If `nil`, consider the download as being successful.
:param: location The location where the downloaded file can be found.
- parameter download: The download that received a chunk of data.
- parameter error: An eventual error. If `nil`, consider the download as being successful.
- parameter location: The location where the downloaded file can be found.
*/
func download(download: TCBlobDownload, didFinishWithError error: NSError?, atLocation location: NSURL?)
}

// MARK: Printable

extension TCBlobDownload: Printable {
extension TCBlobDownload: CustomStringConvertible {
public var description: String {
var parts: [String] = []
var state: String
Expand All @@ -159,11 +171,11 @@ extension TCBlobDownload: Printable {
}

parts.append("TCBlobDownload")
parts.append("URL: \(self.downloadTask.originalRequest.URL)")
parts.append("URL: \(self.downloadTask.originalRequest!.URL)")
parts.append("Download task state: \(state)")
parts.append("destinationPath: \(self.directory)")
parts.append("fileName: \(self.fileName)")

return join(" | ", parts)
return parts.joinWithSeparator(" | ")
}
}
Loading