SwiftHydroPbox下载进度
我用这段代码下载了一个文件,效果很好SwiftHydroPbox下载进度,swift,dropbox-api,Swift,Dropbox Api,我用这段代码下载了一个文件,效果很好 // Download a file let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in let fileManager = NSFileManager.defaultManager() let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory,
// 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)
}
client.files.download(path: "/hello.txt", destination: destination).response { response, error in
if let (metadata, url) = response {
print("*** Download file ***")
let data = NSData(contentsOfURL: url)
print("Downloaded file name: \(metadata.name)")
print("Downloaded file url: \(url)")
print("Downloaded file data: \(data)")
} else {
print(error!)
}
}
我的问题是如何获取下载进度以显示给用户?改编自,您可以在下载
方法上添加进度
回调以获取进度信息:
// 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。改编自,您可以在下载
方法上添加进度
回调以获取进度信息:
// 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。在Swift3上,Dropbox API V2已更新。进度作为NSProgress更新为progressData Dropbox官方信息 NSProgress苹果API参考 所以Greg的样本会像这样更新
dropboxClient.files.files.download(path: "/path/to/Dropbox/file", destination: destination)
.progress { progressData in
print("bytesRead = totalUnitCount: \(progressData.totalUnitCount)")
print("totalBytesRead = completedUnitCount: \(progressData.completedUnitCount)")
print("totalBytesExpectedToRead (Has to sub): \(progressData.totalUnitCount - progressData.completedUnitCount)")
print("progressData.fractionCompleted (New) = \(progressData.fractionCompleted)")
}
.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!)
}
}在Swift3上,Dropbox API V2已更新。进度作为NSProgress更新为progressData Dropbox官方信息 NSProgress苹果API参考 所以Greg的样本会像这样更新
dropboxClient.files.files.download(path: "/path/to/Dropbox/file", destination: destination)
.progress { progressData in
print("bytesRead = totalUnitCount: \(progressData.totalUnitCount)")
print("totalBytesRead = completedUnitCount: \(progressData.completedUnitCount)")
print("totalBytesExpectedToRead (Has to sub): \(progressData.totalUnitCount - progressData.completedUnitCount)")
print("progressData.fractionCompleted (New) = \(progressData.fractionCompleted)")
}
.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!)
}
}在swift 3中,我们应该下载以下格式
let path = "/Files/\(textTitle).txt"
_ = client?.files.download(path: path).response{response,error in
if let response = response{
let responseMetadata = response.0
let filecontents = response.1 . // File in encrypt format
let description = String(data: filecontents, encoding: String.Encoding.utf8)! //Decrypting the file
}else if let error = error{
print("The Download error is \(error)")
}
希望对您有所帮助。在swift 3中,我们应该按以下格式下载
let path = "/Files/\(textTitle).txt"
_ = client?.files.download(path: path).response{response,error in
if let response = response{
let responseMetadata = response.0
let filecontents = response.1 . // File in encrypt format
let description = String(data: filecontents, encoding: String.Encoding.utf8)! //Decrypting the file
}else if let error = error{
print("The Download error is \(error)")
}
希望能有帮助