I was wondering if someone could help me out. I'm trying to use NSURLSessionDownloadTask to display a picture in my UIImageView if I put the image URL into my textfield.
-(IBAction)go:(id)sender {
NSString* str=_urlTxt.text;
NSURL* URL = [NSURL URLWithString:str];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
NSURLSession* session = [NSURLSession sharedSession];
NSURLSessionDownloadTask* downloadTask = [session downloadTaskWithRequest:request];
}
I am not sure where to go after this.
Two options:
Use
[NSURLSession sharedSession]
, with rendition ofdownloadTaskWithRequest
with thecompletionHandler
. For example:Clearly, do whatever you want with the downloaded file (put it somewhere else if you want), but this might be the basic pattern
Create
NSURLSession
usingsession:delegate:queue:
and specify yourdelegate
, in which you'll conform toNSURLSessionDownloadDelegate
and handle the completion of the download there.The former is easier, but the latter is richer (e.g. useful if you need special delegate methods, such as authentication, detecting redirects, etc., or if you want to use background session).
By the way, don't forget to
[downloadTask resume]
, or else the download will not start.