I've have the below methods and NSURLConnection delegates in my project which is currently used for all the request and works fine. Now I'm trying to migrate it to NSURLSession but having issues. Below are both the codes
Original code using NSURLConnection
-(void)startAsynchronousRequestWithoutTLSHandshakeWithComplitionHandler:(MyCompletitionBlock)complitionBlock {
if (self.postData != nil) {
[self injectChallengeBytesIfNeeded:self.postData];
}
[NSURLConnection sendAsynchronousRequest:[self createRequest] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
complitionBlock(response,data,connectionError);
}];
}
-(void)startAsynchronousRequestWithoutTLSHandshake {
if (self.postData != nil) {
[self injectChallengeBytesIfNeeded:self.postData];
}
NSURLConnection *connection = [NSURLConnection connectionWithRequest:[self createRequest] delegate:self];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
receivedData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if ([delegate respondsToSelector:@selector(myWebServiceManager:didRecieveError:)]) {
[delegate myWebServiceManager:self didRecieveError:error];
}
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
LogInfo(@"Response: %@", [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]);
if ([self checkChallengeBytesIfNeeded:receivedData]) {
if ([delegate respondsToSelector:@selector(myWebServiceManager:didRecieveData:)]) {
[delegate myWebServiceManager:self didRecieveData:receivedData];
}
}
else {
NSError *error = [NSError errorWithDomain:@"Server Error" code:-1 userInfo:@{@"Error":@"Security Failure"}];
if ([delegate respondsToSelector:@selector(myWebServiceManager:didRecieveError:)]) {
[delegate myWebServiceManager:self didRecieveError:error];
}
}
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURLCredential *cred = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
}
else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) {
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
}
Below is the one that I tried to migrate to NSURLSession
-(void)startAsynchronousRequestWithoutTLSHandshakeWithComplitionHandler:(MyCompletitionBlock)complitionBlock {
[self createSession];
if (self.postData != nil) {
[self injectChallengeBytesIfNeeded:self.postData];
}
NSURLSessionDataTask *task = [session dataTaskWithRequest:[self createRequest]
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *connectionError) {
complitionBlock(response,data,connectionError);
}];
[task resume];
}
-(void)startAsynchronousRequestWithoutTLSHandshake {
[self createSession];
if (self.postData != nil) {
[self injectChallengeBytesIfNeeded:self.postData];
}
NSURLSessionDataTask *task = [session dataTaskWithRequest:[self createRequest]];
[task resume];
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
receivedData = [[NSMutableData alloc] init];
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (error)
{
if ([delegate respondsToSelector:@selector(myWebServiceManager:didRecieveError:)]) {
[delegate myWebServiceManager:self didRecieveError:error];
}
}
else
{
// The request is complete and data has been received
LogInfo(@"Response: %@", [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]);
if ([self checkChallengeBytesIfNeeded:receivedData]) {
if ([delegate respondsToSelector:@selector(myWebServiceManager:didRecieveData:)]) {
[delegate myWebServiceManager:self didRecieveData:receivedData];
}
}
else {
NSError *error = [NSError errorWithDomain:@"Server Error" code:-1 userInfo:@{@"Error":@"Security Failure"}];
if ([delegate respondsToSelector:@selector(myWebServiceManager:didRecieveError:)]) {
[delegate myWebServiceManager:self didRecieveError:error];
}
}
}
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask willCacheResponse:(NSCachedURLResponse *)proposedResponse completionHandler:(void (^)(NSCachedURLResponse * _Nullable))completionHandler
{
// Return nil
}
-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURLCredential *cred = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
}
else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) {
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
}
Now the problem here is the NSURLSession doesn't seem to call its delegates, not sure what I need to modify. Any help is appreciated