NSUrlConnection 委托方法未从助手 class 中调用
NSUrlConnection Delegate methods are not getting called from helper class
我必须进行 SSL 固定,因此需要验证服务器端 SSL 证书。
所以我必须使用 NSURL 委托。我有一个助手 class,我在其中创建了 returns 我登录响应的方法:
- (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 的 class 调用此方法,代码为
-(void)doLogin
{
self.service = [[SyncCommunicationService alloc]init];
NSData *data = [self.service sendSynchronousRequest:strNewLoginRequest
returningResponse:&response
error:nil];
}
我已经尝试在后台和主线程上调用此方法,但仍然没有调用委托方法,我已经尝试了同一网站上的许多其他答案,但仍然无法解决此问题,所以请问有没有人可以提示我做错了什么。
我想知道为什么有人会使用异步请求来同步执行任务?更不用说这种用 while 语句而不是 dispatch_semaphore 或类似的东西来等待的奇怪方式了。
但是,你为什么还要费心委托呢?只需使用 class 方法 sendSynchronousRequest:returningResponse:error:.我想,你的情况就足够了
我必须进行 SSL 固定,因此需要验证服务器端 SSL 证书。 所以我必须使用 NSURL 委托。我有一个助手 class,我在其中创建了 returns 我登录响应的方法:
- (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 的 class 调用此方法,代码为
-(void)doLogin
{
self.service = [[SyncCommunicationService alloc]init];
NSData *data = [self.service sendSynchronousRequest:strNewLoginRequest
returningResponse:&response
error:nil];
}
我已经尝试在后台和主线程上调用此方法,但仍然没有调用委托方法,我已经尝试了同一网站上的许多其他答案,但仍然无法解决此问题,所以请问有没有人可以提示我做错了什么。
我想知道为什么有人会使用异步请求来同步执行任务?更不用说这种用 while 语句而不是 dispatch_semaphore 或类似的东西来等待的奇怪方式了。
但是,你为什么还要费心委托呢?只需使用 class 方法 sendSynchronousRequest:returningResponse:error:.我想,你的情况就足够了