Closed mahadshahib closed 6 years ago
It does. It will resume the download and once completed didComplete gets called. I suggest put print statement to get an idea what happens when you relaunch the app.
i did actually and yet nothig show up in console unless i start download all over again this is my function
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { if error != nil { let err = error as NSError? let resumeData = err?.userInfo[NSURLSessionDownloadTaskResumeData] as? Data print("anotherone") let newtask = session.downloadTask(withResumeData: resumeData!) newtask.resume() } else { print("hichi") } }
shouldn't i do something on app delegate didlucnchedwithoption function?
You are doing it wrong in delegate. If you check the repo code you will notice that you resume the data in URLSessionDelegate and not in URLSessionTaskDelegate.
And there is commented code you need to put in AppDelegate.
In URLSessionDelegate you get callback when you relaunch the app and once download is completed you get completion call in URLSessionTaskDelegate.
can describe whaat should i do now?
Did you try using the class from repo ? I guess it's self explanatory. I suggest you to run the repo code and check if that's working for you or not.
And again please put resume code in URLSessionDelegate.
I am trying to build my class from your code so yea it helps but I just don't get you when you say with code in urlsessiondelegate where is the urlsesssiondelegate i make my view controller inherit from urlsessiondelegate
`import UIKit
class ViewController: UIViewController , URLSessionDelegate , URLSessionTaskDelegate {
var data : Data? = nil
var task : URLSessionDownloadTask!
var session : URLSession!
let config = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background")
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func start(_ sender: Any) {
beingDownload()
}
@IBAction func stop(_ sender: Any) {
task.suspend()
}
func beingDownload() {
startfresh()
}
func startfresh() {
session = URLSession(configuration: config , delegate: self, delegateQueue: OperationQueue())
let url = URL(string: "https://nava.ir/wp-content/uploads/2018/08/Gholamreza-Sanatgar-Dorooghe-Sefid-128.mp3")
task = session.downloadTask(with: url!)
task.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
debugPrint("Progress \(downloadTask) \(progress)")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
debugPrint("Download finished: \(location)")
try? FileManager.default.removeItem(at: location)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if error != nil {
let err = error as NSError?
let resumeData = err?.userInfo[NSURLSessionDownloadTaskResumeData] as? Data
print("anotherone")
let newtask = session.downloadTask(withResumeData: resumeData!)
newtask.resume()
}
else {
print("hichi")
}
}
}
` this is my viewcontroller
`class ViewController: UIViewController , URLSessionDelegate , URLSessionTaskDelegate , URLSessionDownloadDelegate {
var data : Data? = nil
var task : URLSessionDownloadTask!
var session : URLSession!
let config = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background")
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print("kbkjn")
}
@IBAction func start(_ sender: Any) {
beingDownload()
}
@IBAction func stop(_ sender: Any) {
task.suspend()
}
func beingDownload() {
startfresh()
}
func startfresh() {
session = URLSession(configuration: config , delegate: self, delegateQueue: OperationQueue())
let url = URL(string: "https://nava.ir/wp-content/uploads/2018/08/Gholamreza-Sanatgar-Dorooghe-Sefid-128.mp3")
task = session.downloadTask(with: url!)
task.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
debugPrint("Progress \(downloadTask) \(progress)")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
debugPrint("Download finished: \(location)")
try? FileManager.default.removeItem(at: location)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if error != nil {
let err = error as NSError?
let resumeData = err?.userInfo[NSURLSessionDownloadTaskResumeData] as? Data
print("anotherone")
let newtask = session.downloadTask(withResumeData: resumeData!)
newtask.resume()
}
else {
print("hichi")
}
}
}` i add 3 delegates and nothing can you edit my code
i did but same my problem is that this didcomplete func wont run on idk viewdid load only runs when i hit start again and yea it resmes previos download but start a new one as well
Yeah you should make a another singleton class and move code from controller. The problem I am guessing is in initializing the session. So better make another class to download your files and see the diff.
howw caan i get download progress with your class?
Please check usage I have written in Readme file. You need to assign callbacks in your view controller.
@mahadshahib Did you try using my class ? If that is working you can close this issue.
I am closing this issue, if problem exists try my repo as it is.
wwhen i terminate app and relunch it again how should i call didcomplete cause i think it wont call on it self when i press start download again it ill start but it will start 2 ddownloads one from beginning one resumed