在NSStream上设置SSL或TLS安全性

时间:2014-02-17 13:58:46

标签: objective-c networking ssl nsstream

我有一个使用NSStream的网络代码,我想添加安全性。我试图在这个主题上应用the documentation。我在工作网络代码中添加了以下几行:

[input setValue:NSStreamSocketSecurityLevelNegotiatedSSL forKey:NSStreamSocketSecurityLevelKey];
[output setValue:NSStreamSocketSecurityLevelNegotiatedSSL forKey:NSStreamSocketSecurityLevelKey];

初始化代码是:

-(Connection*) initWithInput:(NSInputStream*) input Output:(NSOutputStream*) output Delegate: (id <ConnectionDelegate>) delegate{
self = [super init];
if (self) {

    a_delegate=delegate;

    //set security
    [input setValue:NSStreamSocketSecurityLevelNegotiatedSSL forKey:NSStreamSocketSecurityLevelKey];
    [output setValue:NSStreamSocketSecurityLevelNegotiatedSSL forKey:NSStreamSocketSecurityLevelKey];

    a_input=input;
    a_output=output;

    //Set delegate to self
    [a_input setDelegate:self];
    [a_output setDelegate:self];

    //Schedule in run loop
    [a_input scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [a_output scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    //open streams
    [a_input open];
    [a_output open];

}
return self;
}

但是当我的Connection对象初始化时,我收到一条错误消息,并且网络不起作用:

[<__NSCFInputStream 0x60800028e970> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key kCFStreamPropertySocketSecurityLevel.

我有什么问题吗?

1 个答案:

答案 0 :(得分:4)

好的我发现了这个问题,我应该使用setProperty:forKey:而不是setValue:forKey: