NSUrlConnection Delegate methods are not getting called from helper class

203 views Asked by At

I have to do SSL pinning so need to verify server side SSL certificate. SO I have to use NSURL delegates. I have a helper class in which I have created method which returns me login response:

- (NSData *)sendSynchronousRequest:(NSString *)strNewLoginRequest
             returningResponse:(NSURLResponse **)response
                         error:(NSError **)error {
NSMutableURLRequest *finalRequest = nil;

NSURL *url= [NSURL URLWithString:const_url];

finalRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];

NSData *requestData = [NSData dataWithBytes:[strLoginRequest UTF8String] length:[strLoginRequest length]];

    self.connection = [[NSURLConnection alloc] initWithRequest:finalRequest delegate:self startImmediately:NO];

    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    [self.connection unscheduleFromRunLoop:currentRunLoop forMode:NSDefaultRunLoopMode];
    [self.connection scheduleInRunLoop:currentRunLoop forMode:@"connectionRunLoopMode"];

    [self.connection start];

    while ([currentRunLoop runMode:@"connectionRunLoopMode" beforeDate:[NSDate distantFuture]]);

    return self.mutableResponse;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.response = response;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    self.mutableResponse = [[NSMutableData alloc]init];
    [self.mutableResponse appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    dispatch_async(dispatch_get_main_queue(), ^{

    if (loadingView)
    {
        [loadingView removeView];
    }

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Failure" message:@"Network Failure" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alert show];
    });
}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (loadingView)
    {
        [loadingView removeView];
    }
    self.resultString = [[NSString alloc] initWithData:self.mutableResponse      encoding:NSASCIIStringEncoding];
}

and I am calling this method from another class called ViewController with code

-(void)doLogin
{
 self.service = [[SyncCommunicationService alloc]init];
 NSData *data = [self.service sendSynchronousRequest:strNewLoginRequest
                                  returningResponse:&response
                                              error:nil];
}

I have tried calling this method in background and on main thread but still delegate methods are not getting called, I have tried many other answers from same website but still couldn't able to solve this issue so please can anybody have a clue what am I doing wrong.

1

There are 1 answers

11
Sergii Martynenko Jr On BEST ANSWER

I'm wondering why would anyone use asynchronous request for performing task synchronously? Not to mention this strange way to wait with while statement instead of dispatch_semaphore or something similar.

However, why You even bother with delegate? Just use class method sendSynchronousRequest:returningResponse:error:. I think, it would suffice in your case