0

我正在使用 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;

}

错误:“NSURLRequest”没有可见的@interface 声明选择器“setSR_SSLPinnedCertificates:”

我错过了什么吗?

谢谢!

4

2 回答 2

0

你需要#import "SRWebSocket.h".

SR_SSLPinnedCertificates是https://github.com/square/SocketRocket/blob/master/SocketRocket/SRWebSocket.hNSURLRequest (CertificateAdditions)中类别的属性

于 2015-05-17T21:23:48.590 回答
0

request需要是一种类型NSMutableURLRequest。当你request是一个NSURLRequest类型时,它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];

这应该有效:)

于 2015-10-10T07:56:56.680 回答