SSL or TLS Security Configuration for NSStream

I have network code that uses NSStream and I want to add security. I tried to apply the documentation on this. I added the following lines to my working network code:

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

Initialization Code:

-(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;
}

But when my Connection object is initialized, an error message appears, and network connectivity does not work:

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

Is there something I'm doing wrong?

+3
source share
1 answer

Ok, I found the problem, I have to use setProperty: forKey: instead of setValue: forKey:

+4
source

All Articles