get file path when downloading with NSURLSession

2019-09-07 13:43发布

问题:

I want to play music while downloading, so i play music with local file, how can i get file path in tmp directory while downloading with NSURLSession.

The method URLSession:downloadTask:didFinishDownloadingToURL: than only call while file downloaded, so what should i do

回答1:

If you want to get file location as it's being downloaded, just don't use download task. For example, create a NSURLSessionDataTask and stream it to persistent storage yourself (e.g. create your own NSOutputStream for local file in persistent storage and open it, and then in didReceiveData, just write the data to your stream, and close the stream when you're all done). Then you can do whatever else you want with the stream/file. If using AFNetworking, I believe that the AFHTTPRequestOperation exposes an outputStream property for this purpose, too.



回答2:

Just like what @matt said, before the download ends you could do nothing except wait. But you could consider use AudioStreamer to play your music while downloading.