I've always used completion handlers. With NSURLConnection
and now with NSURLSession
. It's led to my code being really untidy, especially I have request within request within request.
I wanted to try using delegates in NSURLSession
to implement something I've done untidily with NSURLConnection
.
So I created a NSURLSession
, and created a dataTask
:
NSURLSessionDataTask *dataTask = [overallSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
Right now I have a completionHandler
for the response, how would I switch to delegates to manage the response and data? And can I add another dataTask
from the delegate of this one? Using the cookies that this dataTask
created and placed into the session?