NSURLConnection使异步调用aSync并处理自签名证书

时间:2014-01-10 05:09:47

标签: ios nsurlconnection nsrunloop

我正在尝试进行异步调用,即同步调用。我知道这不是一个更好的主意。但是,我确实需要这样的代码来处理自签名证书的Auth Challenge,同时保持呼叫仍然是同步的。

但是,我不确定这是否是让Asycnh称为Synch的完美方式。

-(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;
}

我还想过使用while循环,如下所示,应该使用哪一个?

   while(!isFinished ){
   }

2 个答案:

答案 0 :(得分:1)

实际上恰恰相反。如果要处理这些NSURLConnectionDelegate方法,则需要使用异步调用,而不是同步。否则代表们永远不会被召唤。

答案 1 :(得分: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);
}