Reputation: 273
I am trying to download some JSON from an API. Right now nothing gets printed.
I know for sure that the path and the API Key/Value works, because I did it with the NSURLSessionDataTask with the completion handler, but I wanted to learn about how to do the same with delegate methods.
But I cant seem to figure out why my delegate methods are not called.
@interface MetaData () <NSURLSessionDelegate, NSURLSessionDataDelegate>
@end
@implementation MetaData
-(void)downloadData
{
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
[sessionConfig setHTTPAdditionalHeaders:@{ header : key}];
NSURLSession *defaultSesh = [NSURLSession sessionWithConfiguration:sessionConfig];
NSURLSessionDataTask *dataTask = [defaultSesh dataTaskWithURL:[NSURL URLWithString:path]];
defaultSesh = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
[dataTask resume];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
NSLog(@"here 2");
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSLog(@"here");
_weaponDictionary = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:nil];
NSLog(@"dictionary: %@", _weaponDictionary);
}
Thanks for the help.
EDIT
defaultSesh = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
by adding that line, i set the delegate to self. So now I print "here 2" but still dont print the dictionary...
Upvotes: 0
Views: 6038
Reputation: 131
Faced a similar issue and was stuck for a really long time when I finally got the answer. Hope it helps for those who search for a similar issue
Swift
// create a URLSessionConfiguration object
`let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData
mySession = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)`
// make a request
`let myTask = mySession.dataTask(with: request as URLRequest , completionHandler: { (data: Data?, response: URLResponse?, error:Error?) in
if let error = error {
print(error.localizedDescription)
}
else{
print(response)
}
})
myTask.resume()`
// if there is a redirect required implement the delegate method
`func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
var newRequest = URLRequest(url:(request.url?.absoluteURL)!)`
// take the URL request from the delegate method and make a new task to begin
`let newDataTask = nestSession.dataTask(with: newRequest)
newDataTask.resume()
}`
// implement the delegate methods to capture the data
`func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
NSLog("task data: %@", data as NSData)
}`
Upvotes: 0
Reputation: 137
You have to set delegate first in viewDidload
id <NSURLSessionDelegate> delegate = [[Your_ViewController alloc]init];
Upvotes: -1
Reputation: 285079
Use the method
+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration
delegate:(id<NSURLSessionDelegate>)delegate
delegateQueue:(NSOperationQueue *)queue
to set the delegate properly.
Update:
And you have to call the completion handler of didReceiveResponse
completionHandler
A completion handler that your code calls to continue the transfer, passing a constant to indicate whether the transfer should continue as a data task or should become a download task.
• If you passNSURLSessionResponseAllow
, the task continues normally.
• If you passNSURLSessionResponseCancel
, the task is canceled.
• If you passNSURLSessionResponseBecomeDownload
as the disposition, your delegate’sURLSession:dataTask:didBecomeDownloadTask:
method is called to provide you with the new download task that supersedes the current task.
For example
- (void) URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
completionHandler(NSURLSessionResponseAllow);
}
Upvotes: 5