Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中查找文件的下载进度_Ios_Swift_Download_Progress_Nsurlsession - Fatal编程技术网

Ios 在swift中查找文件的下载进度

Ios 在swift中查找文件的下载进度,ios,swift,download,progress,nsurlsession,Ios,Swift,Download,Progress,Nsurlsession,我已经搜索过了,但只在目标C中没有找到相关答案。有没有办法在Swift中找到文件下载的进度,以便向用户显示?我是iOS编程新手,我尝试过NSURLSession,但没有成功 编辑: 我使用了这一方法,正如在帖子中看到的,但我似乎不明白如何获得进度状态: func downloadFile(page: NSString){ finished = false var statusCode:Int = 0 println("Download starting") let

我已经搜索过了,但只在目标C中没有找到相关答案。有没有办法在Swift中找到文件下载的进度,以便向用户显示?我是iOS编程新手,我尝试过NSURLSession,但没有成功

编辑: 我使用了这一方法,正如在帖子中看到的,但我似乎不明白如何获得进度状态:

func downloadFile(page: NSString){
    finished = false
    var statusCode:Int = 0
    println("Download starting")
    let url = NSURL(string: page)

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in

        if error != nil {
            println("download failed with error \(error?.localizedDescription)")
        } else {
            println("Expected Content-Length \(response.expectedContentLength)")
            self.contentLength = response.expectedContentLength
            if let httpResponse = response as? NSHTTPURLResponse {
                println("Status Code of number \(self.countDownload) is \(httpResponse.statusCode)")
                statusCode = httpResponse.statusCode
            }
        }
    }
    task.resume()
}

提前感谢您

假设您正在下载一个文件,那么就有一个名为
NSURLSessionDownloadTask
的子类。以下是NSURLSession文档中关于特定功能的摘录:

定期通知学员下载进度

func URLSession(_ session: NSURLSession,
    downloadTask downloadTask: NSURLSessionDownloadTask,
    didWriteData bytesWritten: Int64,
    totalBytesWritten totalBytesWritten: Int64,
    totalBytesExpectedToWrite totalBytesExpectedToWrite: Int64
)
例如,您可以通过执行以下操作将进度输出到控制台:

println("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")

可以在中计算进度状态

URLSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)
这是protocolNSURLSessionDownloadDelegate所需的三种方法之一。在我的例子中,该方法的代码如下所示:

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    // println("download task did write data")

    let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)

    dispatch_async(dispatch_get_main_queue()) {
        self.progressDownloadIndicator.progress = progress
    }
}
我创建了一个小项目,它实现了三种不同的方法:

  • 同步下载
  • 异步下载
  • 随进度下载

查看:

您只需观察
URLSessionDataTask
对象的
progress
属性即可。你不需要像其他答案所建议的那样计算进度。
Progress
上有一个
fractionCompleted
属性

游乐场示例:

<代码>导入基础 导入PlaygroundSupport 让page=PlaygroundPage.current page.needsIndefiniteExecution=true 让url=url(字符串:https://source.unsplash.com/random/4000x4000")! 让task=URLSession.shared.dataTask(带:url){ 第页完成执行() } //当你不再需要观察时,别忘了让它失效。 让observation=task.progress.observate(\.fractionCompleted){progress,\ in 打印(进度。已完成分段) } task.resume()
您是如何尝试使用
NSURLSession
的?它做错了什么?向我们展示您尝试过的东西我相信只有在使用委托方法而不是闭包的情况下,这才有效。@dar512这是真的。我应该注意到,如果使用闭包,我不相信有任何方法可以找到进展。