使用 SwiftyDropbox 库下载包含进度信息的文件

改编自本教程 ,它使用 SwiftyDropbox 库下载文件,在下载方法上使用进度回调来获取进度信息:

// Download a file
let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in
    let fileManager = NSFileManager.defaultManager()
    let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    // generate a unique name for this file in case we've seen it before
    let UUID = NSUUID().UUIDString
    let pathComponent = "\(UUID)-\(response.suggestedFilename!)"
    return directoryURL.URLByAppendingPathComponent(pathComponent)
}

Dropbox.authorizedClient!.files.download(path: "/path/to/Dropbox/file", destination: destination)

    .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in

        print("bytesRead: \(bytesRead)")
        print("totalBytesRead: \(totalBytesRead)")
        print("totalBytesExpectedToRead: \(totalBytesExpectedToRead)")

    }

    .response { response, error in

        if let (metadata, url) = response {
            print("*** Download file ***")
            print("Downloaded file name: \(metadata.name)")
            print("Downloaded file url: \(url)")
        } else {
            print(error!)
        }

    }

然后,你可以使用该原始进度信息来备份应用程序中的进度 UI。