Ich habe eine Download-Funktion funktioniert perfekt, auf Klick auf meine Schaltfläche, wird die Datei korrekt heruntergeladen werden.Download auf Hintergrund/anderen Bildschirm auf meiner iOS-App fortsetzen
Was ich erreichen möchte, wenn ich die App in den Hintergrund stellen oder den Bildschirm wechseln, während sie die Datei herunterlädt, möchte ich, dass der Download fortgesetzt wird. Derzeit, wenn die App im Hintergrund ist, muss der Download gestoppt und der Benutzer muss ihn von Null neu starten.
Wie ist das möglich ?? Unten ist meine Download-Funktion, ich habe versucht, einen Hintergrund-Download ohne Erfolg hinzuzufügen.
func createDownloadTask() {
let downloadRequest = URLRequest(url: URL(string: "\(posts[selectedIndexPath].link)")!)
let session = Foundation.URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
downloadTask = session.downloadTask(with: downloadRequest)
downloadTask!.resume()
}
und
@IBAction func startDownload(_ sender: UIButton) {
let urlString = "\(posts[selectedIndexPath].link)"
DispatchQueue.global(qos: .default).async(execute: {
print("downloadVideo");
let url=NSURL(string: urlString);
let urlData=NSData(contentsOf: url! as URL);
if((urlData) != nil)
{
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let fileName = urlString as NSString;
let filePath="\(documentsPath)/\(fileName.lastPathComponent)";
DispatchQueue.main.async(execute: {() -> Void in
print(filePath)
urlData?.write(toFile: filePath, atomically: true);
print("video Saved to document directory of app");
self.downloadButton.alpha = 0;
self.progressView.alpha = 0;
self.cardboardButton.alpha = 1;
self.videoButton.alpha = 1;
self.removefile.alpha = 1;
})
}
})
}