这是执行此操作的正确方法。虽然这样做(在套接字连接后设置属性)没有记录,但这是直接来自我的 Monal xmpp 客户端的代码,苹果从未在应用商店中给我任何问题。
NSInputStream *iStream;
NSOutputStream *oStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)server, port, &iStream, &oStream);
[iStream open];
[oStream open];
一旦连接打开并且你得到 NSStreamEventOpenCompleted 并且 startTLS 命令已经从客户端发送到主机:
NSDictionary *settings = [ [NSDictionary alloc ]
initWithObjectsAndKeys:
[NSNumber numberWithBool:YES], @"kCFStreamSSLAllowsExpiredCertificates",
[NSNumber numberWithBool:YES], @"kCFStreamSSLAllowsExpiredRoots",
[NSNumber numberWithBool:YES], @"kCFStreamSSLAllowsAnyRoot",
[NSNumber numberWithBool:NO], @"kCFStreamSSLValidatesCertificateChain",
[NSNull null],@"kCFStreamSSLPeerName",
@"kCFStreamSocketSecurityLevelNegotiatedSSL",
@"kCFStreamSSLLevel",
nil ];
CFReadStreamSetProperty((CFReadStreamRef)iStream,
@"kCFStreamPropertySSLSettings", (CFTypeRef)settings);
CFWriteStreamSetProperty((CFWriteStreamRef)oStream,
@"kCFStreamPropertySSLSettings", (CFTypeRef)settings);