AWSS3 Mobile SDK 在 swift 取消文件下载
AWSS3 Mobile SDK cancel file download in swift
我的应用程序中有一个使用 AWS Mobile SDK 的文件下载代码。
这会显示带有下载进度和取消按钮的警报。它正在工作,但当用户点击警报取消按钮时我需要取消下载。我尝试使用 AWSS3TransferUtilityDownloadTask 的对象取消任务,AWSS3TransferUtilityTask.But 它似乎不起作用,它让我取得进展并完成下载。
这里是我的文件下载工作代码。
请建议我如何取消我的下载请求。
let credentialProvider = AWSStaticCredentialsProvider(accessKey: "MyAccessKey", secretKey: "MySecretKey")
let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
// Do something e.g. Update a progress bar.
print("File Progress:",Float(progress.fractionCompleted))
let progress = Float(progress.fractionCompleted)
DispatchQueue.main.async {
if progress == 1.0 {
self.globalAlert.dismiss(animated: true, completion: nil)
}else{
self.globalProgressView!.progress = progress
self.msgProgress = String(Int(progress*100))
}
}
})
}
var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
completionHandler = { (task, URL, data, error) -> Void in
DispatchQueue.main.async(execute: {
// Do something e.g. Alert a user for transfer completion.
// On failed downloads, `error` contains the error object.
//My other code stuff execution
})
}
var refUploadTask: AWSS3TransferUtilityTask?
let transferUtility = AWSS3TransferUtility.default()
transferUtility.downloadData(
fromBucket: Bucket_Name,
key: fileKey,
expression: expression,
completionHandler: completionHandler
).continueWith {
(task) -> AnyObject? in if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let _ = task.result {
// Do something with downloadTask.
print("download started..")
DispatchQueue.main.async(execute: {
self.globalAlert = UIAlertController(title: "Downloading...", message: "\(String(describing: self.msgProgress))% Completed", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: {
(action : UIAlertAction!) -> Void in
self.refUploadTask.cancel()
})
// Show it to your users
self.globalAlert.addAction(cancelAction)
self.present(self.globalAlert, animated: true, completion: {
// Add your progressbar after alert is shown (and measured)
let margin:CGFloat = 8.0
let rect = CGRect(x: margin, y: 72.0, width: self.globalAlert.view.frame.width - margin * 2.0 , height: 2.0)
self.globalProgressView = UIProgressView(frame: rect)
self.globalProgressView!.tintColor = .blue
self.globalAlert.view.addSubview(self.globalProgressView!)
})
})
}
return nil;
}
您可以获取 AWSS3TransferUtilityDownloadTask 对象并取消正在进行的下载。
let awsTask = transferUtility.getDownloadTasks()
if let taskArray = awsTask.result {
for idx in taskArray {
if let task = idx as? AWSS3TransferUtilityDownloadTask {
task.cancel()
}
}
}
我的应用程序中有一个使用 AWS Mobile SDK 的文件下载代码。 这会显示带有下载进度和取消按钮的警报。它正在工作,但当用户点击警报取消按钮时我需要取消下载。我尝试使用 AWSS3TransferUtilityDownloadTask 的对象取消任务,AWSS3TransferUtilityTask.But 它似乎不起作用,它让我取得进展并完成下载。
这里是我的文件下载工作代码。
请建议我如何取消我的下载请求。
let credentialProvider = AWSStaticCredentialsProvider(accessKey: "MyAccessKey", secretKey: "MySecretKey")
let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
// Do something e.g. Update a progress bar.
print("File Progress:",Float(progress.fractionCompleted))
let progress = Float(progress.fractionCompleted)
DispatchQueue.main.async {
if progress == 1.0 {
self.globalAlert.dismiss(animated: true, completion: nil)
}else{
self.globalProgressView!.progress = progress
self.msgProgress = String(Int(progress*100))
}
}
})
}
var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
completionHandler = { (task, URL, data, error) -> Void in
DispatchQueue.main.async(execute: {
// Do something e.g. Alert a user for transfer completion.
// On failed downloads, `error` contains the error object.
//My other code stuff execution
})
}
var refUploadTask: AWSS3TransferUtilityTask?
let transferUtility = AWSS3TransferUtility.default()
transferUtility.downloadData(
fromBucket: Bucket_Name,
key: fileKey,
expression: expression,
completionHandler: completionHandler
).continueWith {
(task) -> AnyObject? in if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let _ = task.result {
// Do something with downloadTask.
print("download started..")
DispatchQueue.main.async(execute: {
self.globalAlert = UIAlertController(title: "Downloading...", message: "\(String(describing: self.msgProgress))% Completed", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: {
(action : UIAlertAction!) -> Void in
self.refUploadTask.cancel()
})
// Show it to your users
self.globalAlert.addAction(cancelAction)
self.present(self.globalAlert, animated: true, completion: {
// Add your progressbar after alert is shown (and measured)
let margin:CGFloat = 8.0
let rect = CGRect(x: margin, y: 72.0, width: self.globalAlert.view.frame.width - margin * 2.0 , height: 2.0)
self.globalProgressView = UIProgressView(frame: rect)
self.globalProgressView!.tintColor = .blue
self.globalAlert.view.addSubview(self.globalProgressView!)
})
})
}
return nil;
}
您可以获取 AWSS3TransferUtilityDownloadTask 对象并取消正在进行的下载。
let awsTask = transferUtility.getDownloadTasks()
if let taskArray = awsTask.result {
for idx in taskArray {
if let task = idx as? AWSS3TransferUtilityDownloadTask {
task.cancel()
}
}
}