【问题标题】:NSUrlConnection Delegate methods are not getting called from helper classNSUrlConnection 委托方法没有从帮助程序类中调用
【发布时间】:2015-06-18 07:36:33
【问题描述】:

我必须做 SSL pinning,所以需要验证服务器端 SSL 证书。 所以我必须使用 NSURL 代表。我有一个帮助类,我在其中创建了返回登录响应的方法:

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

我正在使用代码从另一个名为 ViewController 的类调用此方法

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

我尝试在后台和主线程中调用此方法,但仍然没有调用委托方法,我尝试了同一网站的许多其他答案,但仍然无法解决此问题,所以请任何人提供线索我做错了什么。

【问题讨论】:

    标签: ios delegates nsurlconnection nsurlconnectiondelegate


    【解决方案1】:

    我想知道为什么有人会使用异​​步请求来同步执行任务?更不用说这种用 while 语句而不是 dispatch_semaphore 或类似的东西来等待的奇怪方式了。

    但是,您为什么还要打扰委托?只需使用类方法 sendSynchronousRequest:returningResponse:error:。我认为,在你的情况下就足够了

    【讨论】:

    • 我必须验证 SSL 固定的服务器证书,所以我必须使用委托方法。
    • 在这种情况下,尝试设置连接委托队列setDelegateQueue: 让这个队列成为您的 SyncCommunicationService 的属性
    • 而且,您对等待操作的while 语句有信心吗?你为什么不使用 dispatch_semaphore 来代替?比您的sendSynchronousRequest:returningResponse:error: 方法不会返回任何内容,并且可以从 SyncCommunicationService 的属性中检索响应数据(这是我每次都在做的,我需要异步检索数据)
    • 是的! while 语句用于等待操作及其工作,如果我从 ViewController 类本身调用所有方法。当我尝试与我的助手类进行通信以获取数据时,就会出现问题。
    • 我也尝试设置委托队列,但仍然没有成功。
    猜你喜欢
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多