swift3 - Resume Data from URLSessionDownloadTask Always nil -
i have app need download file internet when downloading file it's work problem when pressed pause button pause downloading 1 minute or more nil resume data
the following code :
@ibaction func startdownloading(_ sender: uibutton) { isdownload = true sessionconfig = urlsessionconfiguration.default let operationqueue = operationqueue.main session = urlsession.init(configuration: sessionconfig, delegate: self, delegatequeue: operationqueue) let url = url(string: "www.example.com") downloadtask = session.downloadtask(with: url!) downloadtask.resume() } @ibaction func pause(_ sender: uibutton) { if downloadtask != nil && isdownload { self.downloadtask!.cancel(byproducingresumedata: { (resumedata) in // here nil resume data }) isdownload = false downloadtask = nil pasuebtnoutlet.settitle("resume", for: .normal) } if !isdownload && downloaddata != nil { downloadtask = session.downloadtask(withresumedata: downloaddata data) downloadtask.resume() isdownload = true downloaddata = nil pasuebtnoutlet.settitle("pause", for: .normal) } }
please can me
thanks all
your code seems correct, need init downloaddata
resumedata
in closure
make property of downloaddata
var downloaddata:data!
and in pause button action cancel task, set downloaddata
resumedata
self.downloadtask!.cancel(byproducingresumedata: { (resumedata) in // here nil resume data // have set download data resume data self.downloaddata = resumedata })
in order check progress , completion, implement these urlsessiondownloaddelegate
delegates
func urlsession(_ session: urlsession, downloadtask: urlsessiondownloadtask, didwritedata byteswritten: int64, totalbyteswritten: int64, totalbytesexpectedtowrite: int64) { let progress = float(totalbyteswritten) / float(totalbytesexpectedtowrite) print(int(progress * 100)) } func urlsession(_ session: urlsession, downloadtask: urlsessiondownloadtask, didfinishdownloadingto location: url) { print("downloading done") }
note:- try valid downloadable url. example http://www.star.uclan.ac.uk/news_and_events/news/2010020901/sdo_resolution_comparison.png
its http, make sure set transport security in info.plist
Comments
Post a Comment