ios - 使用 ssl 的同步子类 NSURLConnection?

标签 ios objective-c ssl nsurlconnection synchronous

我将 NSURLConnection 子类化,以便使用 ssl 调用网络服务。 我考虑过同步连接,而不仅仅是像现在这样的异步连接。

我的代码:

#import "SecureSSLConnection.h"

static NSMutableArray *sharedConnectionList = nil;

@implementation SecureSSLConnection
@synthesize request, completionBlock, internalConnection;

- (id)initWithRequest:(NSMutableURLRequest *)req
{
    self = [super init];
    if (self) {
        self.request = req;
    }
    return self;
}

- (void)start
{
    container = [NSMutableData new];
    internalConnection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:YES];

    if (!sharedConnectionList) {
        sharedConnectionList = [NSMutableArray new];
    }
    [sharedConnectionList addObject:self];    
}

#pragma mark - NSURLConnectionDelegate

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (self.completionBlock) {
        self.completionBlock(container, nil);
    }

    [sharedConnectionList removeObject:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    if (self.completionBlock) {
        self.completionBlock(nil, error);
    }

    [sharedConnectionList removeObject:self];
}

#pragma mark - SSL Connection Addition

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"challenge.protectionSpace.host: %@", challenge.protectionSpace.host);

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        // We only trust our own domain
        if ([challenge.protectionSpace.host isEqualToString:WebSiteURL]) {
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
        }
    }

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

我开始考虑实现:

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

为了和原来的delegate保持一致。

一种方法是使用 NSNotificationCenter,但我的目标是更简单的方法。

我应该怎么做?

最佳答案

我不建议同步。您总是希望保持网络请求异步。

简单的解决方案是使用您的完成 block 属性。只需将依赖于您的初始 SSL 请求的其他任务放在第一个请求的完成 block 中。这样一来,它们将在第一个完成之前不会开始。

更优雅的解决方案是更进一步,将您的 SSL 请求包装在并发的 NSOperation 子类中。这样,您不仅可以使用上述完成 block 模式,还可以使用后续操作的 addDependencyNSOperationQueuemaxConcurrentOperationCount 来真正细化各种操作之间的动态。这很重要,但您可以查看 AFNetworking 作为此模式的相对深思熟虑的实现示例。

关于ios - 使用 ssl 的同步子类 NSURLConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22916578/

相关文章:

ios - 事件指示器在异步查询结束前停止

ios - UIView 上存在哪些类似的功能方法(viewWillAppear)?

ios - 更改文本转语音中对象的描述

ios - 自定义 UITableViewCell 项目无法正确显示

iphone - 如何使用 Core Data 将子查询作为属性执行?

iphone - iOS 应用程序的服务器/网络入门

objective-c - 在 objective-c 中舍入 float

php - SSL 证书配置错误

ssl - "A fatal error occurred while creating a TLS client credential. The internal error state is 10013."每 10 秒

apache - 无法通过 WebDAV 移动或重命名文件