get file path when downloading with NSURLSession

守給你的承諾、 提交于 2019-12-25 14:16:52

问题


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.



来源:https://stackoverflow.com/questions/26748229/get-file-path-when-downloading-with-nsurlsession

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!