How can I link a file download with a Progress View

前端 未结 2 589
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 00:18

My button code below download a file from a URL, I need to link it with a Progress View to show the Downloading Progress.

@IBAction func btnStream(sender: U         


        
2条回答
  •  甜味超标
    2021-01-03 00:45

    Check this tutorial. It's in Objective-C, but it will be easy to convert to Swift.

    The principle is to implement some NSURLConnectionDataDelegatefunctions on your VC :

    • connection:didReceiveResponse -> You can retrieve the size of the file that will be downloaded and estimate the download percentage with it.
    • connection:didReceiveData -> It's the refresh function, it will be called multiple times during the download. You can compare the size of your incomplete NSData object with the size of the file.
    • connectionDidFinishLoading -> This method is called at the end of the download process.

    Hope it helps, and don't hesitate to comment if you have some troubles converting Obj-C to Swift.

提交回复
热议问题