1

I am fetching data using NSURLSession but I don't know how to get the data back once the download is complete my code :

class ViewController: UIViewController,NSURLSessionDelegate,NSURLSessionDataDelegate{

@IBOutlet var imageView: UIImageView!
@IBOutlet weak var progress: UIProgressView!

var buffer:NSMutableData = NSMutableData()
var session:NSURLSession?
var dataTask:NSURLSessionDataTask?
let url = NSURL(string:"https://initiate.alphacoders.com/download/wallpaper/566046/images7/jpg/37100/1353" )!
var expectedContentLength = 0
var downloadedImage = UIImage()
var downloadedData = NSData()


override func viewDidLoad() {
    super.viewDidLoad()
    progress.progress = 0.0
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    let manqueue = NSOperationQueue.mainQueue()
    session = NSURLSession(configuration: configuration, delegate:self, delegateQueue: manqueue)
    dataTask = session?.dataTaskWithRequest(NSURLRequest(URL: url))
    dataTask?.resume()

    // Do any additional setup after loading the view, typically from a nib.
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {

    //here you can get full lenth of your content
    expectedContentLength = Int(response.expectedContentLength)
    print(expectedContentLength)
    completionHandler(NSURLSessionResponseDisposition.Allow)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {

    if progress.progress > 0.99 {

        downloadedImage = UIImage(data: data , scale:  1)!
    }
    buffer.appendData(data)

    print("checking")

    let percentageDownloaded = Float(buffer.length) / Float(expectedContentLength)
    progress.progress =  percentageDownloaded
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
    //use buffer here.Download is done
    progress.progress = 1.0// download 100% complete

    imageView.image = downloadedImage
}
}

I tried to get the data from func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData but at didReceiveData my file is not yet fully downloaded and i don't know how to get the data from didCompleteWithError function , am new in swift and still figuring out things here ,

If anybody knows here what I am missing or what should I do then please tell me, any guidance will be so appreciated and helpful.

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
remy boys
  • 2,928
  • 5
  • 36
  • 65

1 Answers1

1
- URLSession:downloadTask:didFinishDownloadingToURL:

Is something you are missing.

Called to inform the delegate that a download task has finished downloading.

This method gives you a file URL for the temporary file (The third parameter). Because the file is temporary, you must either open the file for reading or move it to a permanent location in your app’s sandbox container directory before returning from this delegate method.

Find more here.

EDIT 1: To check the progress override following method:

- URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:

EDIT 2: Okay I see now. Make following changes and see if it works?:

  1. Let your class conform NSURLSessionDownloadDelegate
  2. Change dataTask = session?.dataTaskWithRequest(NSURLRequest(URL: url)) to dataTask = session?.downloadTaskWithURL(NSURLRequest(URL: url))

That should make you download the file.

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
  • hey @vivekMolkar thanks for responding man , i tried to find this method but there's nothing as `didFinishDownloadingToURL` in NSURlSession class ?? did you mean `URLSessionDidFinishEventsForBackgroundURLSession` ??? – remy boys Feb 28 '16 at 14:32
  • 1
    strange, it must be there. see this [link](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html#//apple_ref/doc/uid/TP40013509-SW14) for example. its from apple docs. also plz check if you are missing something. – Vivek Molkar Feb 28 '16 at 14:35
  • hey , i got it there was an typo my bad – remy boys Feb 28 '16 at 14:37
  • but its not returning anything , am i doing it in a wrong way ? `func URLSession(session: NSURLSession, downloadTask downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL){ print("session \(session) has finished the download task \(downloadTask) of URL \(location).") }` – remy boys Feb 28 '16 at 14:38
  • dose the control reach this method? if yes what dose it print? – Vivek Molkar Feb 28 '16 at 14:42
  • its not printing anything it think its not reaching at all – remy boys Feb 28 '16 at 14:45
  • Is the file large? See the edit for progress of download. – Vivek Molkar Feb 28 '16 at 14:49