SocketRocket和固定证书错误?

时间:2015-05-17 14:53:54

标签: objective-c socketrocket

我正在使用SocketRocket,到目前为止一切工作正常,今天我想尝试确定(自签名)证书,但是我收到了错误:

- (void)connectWebSocket {
    webSocket.delegate = nil;
    webSocket = nil;

    NSString *urlString = [NSString stringWithFormat: @"wss://%@:%@", server_ip, server_port];

    //NSLog(@"%@", urlString);

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                             cachePolicy: NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:5.0];


    // pin down certificate
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"myOwnCertificate" ofType:@"cer"];
    NSData *certData = [[NSData alloc] initWithContentsOfFile:cerPath];
    CFDataRef certDataRef = (__bridge CFDataRef)certData;
    SecCertificateRef certRef = SecCertificateCreateWithData(NULL, certDataRef);
    id certificate = (__bridge id)certRef;
    [request setSR_SSLPinnedCertificates:@[certificate]];

    SRWebSocket *newWebSocket = [[SRWebSocket alloc] initWithURLRequest: request];
    newWebSocket.delegate = self;

    [newWebSocket open];

    socketIsOpen = true;

}

错误:没有可见的@interface用于' NSURLRequest'声明选择器' setSR_SSLPinnedCertificates:'

我错过了什么吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要#import "SRWebSocket.h"

SR_SSLPinnedCertificateshttps://github.com/square/SocketRocket/blob/master/SocketRocket/SRWebSocket.h

NSURLRequest (CertificateAdditions)类别的媒体资源

答案 1 :(得分:0)

request必须是NSMutableURLRequest类型。当您的requestNSURLRequest类型时,SR_SSLPinnedCertificates 只读,因此您可以设置它。

#pragma mark - NSURLRequest (CertificateAdditions)

@interface NSURLRequest (CertificateAdditions)

@property (nonatomic, retain, readonly) NSArray *SR_SSLPinnedCertificates;

@end

#pragma mark - NSMutableURLRequest (CertificateAdditions)

@interface NSMutableURLRequest (CertificateAdditions)

@property (nonatomic, retain) NSArray *SR_SSLPinnedCertificates;

@end

将您的代码更改为:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                             cachePolicy: NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:5.0];

这应该有效:)