Reputation: 598
I am creating a new Downloader object (simply to download PDF files) and I need to call a method in my ViewController which show the message that the file is successfully downloaded. I am using the answer by Ahmet Akkök on this question and when I try to use yourOwnObject.showDownloadCompleted()
it can't find the method.
ViewController:
override func viewDidLoad(){
super.viewDidLoad();
let pdfURL = "exampleToPFD.com/mypdf.pdf";
let url = NSURL(string: pdfURL);
let d = Downloader(yourOwnObject: self);
d.download(url!);
}
func showDownloadComplete(){
print("done");
}
Downloader:
import Foundation
class Downloader : NSObject, NSURLSessionDownloadDelegate {
var url : NSURL?
// will be used to do whatever is needed once download is complete
var yourOwnObject : NSObject?
var downloaded = false;
var documentDestination = "";
init(yourOwnObject : NSObject){
self.yourOwnObject = yourOwnObject
}
// is called once the download is complete
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
//copy downloaded data to your documents directory with same names as source file
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first
let destinationUrl = documentsUrl!.URLByAppendingPathComponent(url!.lastPathComponent!)
let dataFromURL = NSData(contentsOfURL: location)
dataFromURL!.writeToURL(destinationUrl, atomically: true)
// now it is time to do what is needed to be done after the download
print("download done...");
// call to the parent method here
documentDestination = destinationUrl.absoluteString;
print("DestURL" + (destinationUrl.absoluteString));
}
// method to be called to download
func download(url: NSURL) {
self.url = url
// download identifier can be customized. I used the "ulr.absoluteString"
let sessionConfig = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(url.absoluteString)
let session = NSURLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil)
let task = session.downloadTaskWithURL(url);
task.resume();
}
}
Upvotes: 2
Views: 593
Reputation: 3956
You can use delegate to achieve that. You can try out following
Downloader.swift
import Foundation
protocol DownloadDelegate {
func downloadCompleted()
}
class Downloader : NSObject, NSURLSessionDownloadDelegate{
var url : NSURL?
var downloadDelegate : DownloadDelegate!
// will be used to do whatever is needed once download is complete
var downloaded = false;
var documentDestination = "";
//is called once the download is complete
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL){
//copy downloaded data to your documents directory with same names as source file
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first
let destinationUrl = documentsUrl!.URLByAppendingPathComponent(url!.lastPathComponent!)
let dataFromURL = NSData(contentsOfURL: location)
dataFromURL!.writeToURL(destinationUrl, atomically: true)
//now it is time to do what is needed to be done after the download
print("download done...");
downloadDelegate. downloadCompleted()
documentDestination = destinationUrl.absoluteString;
print("DestURL" + (destinationUrl.absoluteString));
}
//this is to track progress
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64){
print((String)(totalBytesWritten)+"/"+(String)(totalBytesExpectedToWrite));
}
// if there is an error during download this will be called
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?){
if(error != nil){
//handle the error
print("Download completed with error: \(error!.localizedDescription)");
}
}
//method to be called to download
func download(url: NSURL){
self.url = url
//download identifier can be customized. I used the "ulr.absoluteString"
let sessionConfig = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(url.absoluteString)
let session = NSURLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil)
let task = session.downloadTaskWithURL(url);
task.resume();
}
}
ViewController :
class ViewController: UIViewController,DownloadDelegate{
override func viewDidLoad(){
super.viewDidLoad();
// Do any additional setup after loading the view, typically from a nib.
let pdfURL = "exampleToPFD.com/mypdf.pdf";
let url = NSURL(string: pdfURL);
let d = Downloader();
d.downloadDelegate = self
d.download(url!);
showToast("Download Started...");
}
func downloadCompleted() {
//download completed
}
}
Upvotes: 3