Search code examples
iosnsurlconnectionnsurlsession

Passing response to controller while converting NSURLConnection to NSURLSession


I have something like service API class which implements NSURLConnectionDelegate methods. I noticed that some of the methods of this are deprecated and Apple now suggests to use NSURLSession. I created this service API's own delegate which the calling class would implement and that would be executed when a response is received. I am looking for how I can do something similar while using NSURLSession.

My present stuff with NSURLConnection:

@class ServiceAPI;
@protocol ServiceAPIDelegate <NSObject>

- (void)getResponseData:(NSData *)responseData;

@end

@interface ServiceAPI : NSObject<NSURLCoDelegate>
- (void)httpServiceRequest:(NSMutableURLRequest *)serviceRequest;
@property (nonatomic, weak) id <ServiceAPIDelegate> delegate;
@end

In ServiceAPI implementation file:

- (void)httpServiceRequest:(NSMutableURLRequest *)serviceRequest {
    //[[NSURLSession sharedSession] dataTaskWithRequest:serviceRequest] resume];
    self.requestConnection = [NSURLConnection connectionWithRequest:serviceRequest delegate:self];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.delegate getResponseData:self.responseData sender:self];
}

Class making request and getting response:

@interface CallingController : UITableViewController<ServiceAPIDelegate>
@end

Implementation file CallingController:

- (void)getResponseData:(NSData *)responseData {
   // Do something with response. 
}

How do I let the calling class handle the response method just like NSURLConnection while using NSURLSession. While reading NSURLSession looks like the request and response are handled together using completion handler something like this:

NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=apple&media=software"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"%@", json);
}]; 

I still want to have a service API class and my controller would just pass down the request to service API to make calls and once the response is back, it would be passed down to controller. How do I pass the response to the controller while using NSURLSession.


Solution

  • Something like this:

       @class ServiceAPI;
    
        @protocol ServiceAPIDelegate <NSObject>
    
        - (void)getResponseData:(NSData *)responseData;
    
        @end
    
        @interface ServiceAPI : NSObject<NSURLSessionDelegate>
        - (void)httpServiceRequest:(NSMutableURLRequest *)serviceRequest;
        @property (nonatomic, weak) id <ServiceAPIDelegate> delegate;
        @end
    

    In Implementation

    - (void)httpServiceRequest:(NSMutableURLRequest *)serviceRequest {
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *downloadTask =
    
        [session dataTaskWithRequest:serviceRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    
            [self.delegate getResponseData:data sender:self];
    
        }];
    
        [downloadTask resume];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // no use
    }