0

I am trying to make a Asynchronous Call , a Synchronous one. I know its not a better idea to do it. But, I do need such to code to handle Auth Challenge of Self Signed Certificate while Keeping the call still as Synchronous.

But, I am not sure whether it is a perfect way to make Asycnh call a Synch one.

-(NSData*) startConnection{
     NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

   while(!isFinished && [[NSRunLoop currentLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]){
   }


   return responseAppData;
}


     - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
      //Code to handle Certificate
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
       [responseAppData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
         isFinished=YES;
}

I also thought of using the while Loop as below, so which one should be used?

   while(!isFinished ){
   }
NNikN
  • 3,720
  • 6
  • 44
  • 86

2 Answers2

1

Actually it's the opposite. If you want to handle these NSURLConnectionDelegate methods, you need to use asynchronous calls, NOT synchronous. Otherwise the delegates are never called.

valheru
  • 2,552
  • 3
  • 20
  • 40
  • My code works Asynchronous, responds to delegate and because of while condition it works as Synchronous code. – NNikN Jan 10 '14 at 05:42
0
 typedef void (^onDownload)(NSData *data);

@property (nonatomic,assign) onDownload block;


-(void) startConnectionwithBlock:(onDownload) pBlock;{
    self.block = [pBlock copy];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
    block(self.data);
}
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86