关于2017适配HTTPS 很多人问过苹果官方,回复是还没有收到任何信息,说不用召集配置.但是大家已经认识到了HTTPS的重要性
之前看到过API afnetworking 下配置HTTPS,这里就不说啦
今天说一下webveiw下.我第一反应是懵逼的不知道怎么整.
通过
NSURLProtocol 拦截全部的webview的协议然后把下面的添加进去就好啦 root,cer 就是运维给的证书
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
static CFArrayRef certs;
if (!certs) {
NSData*certData =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"root" ofType:@"cer"]];
// SecCertificateRef rootcert =SecCertificateCreateWithData(kCFAllocatorDefault,CFBridgingRetain(certData));
NSString * cerstr = [[NSString alloc]initWithData:certData encoding:NSUTF8StringEncoding];
if ([cerstr hasPrefix:@"-----BEGIN CERTIFICATE-----"]||[cerstr hasSuffix:@"-----END CERTIFICATE-----"]) {
cerstr = [cerstr stringByReplacingOccurrencesOfString:@"-----BEGIN CERTIFICATE-----" withString:@""];
cerstr = [cerstr stringByReplacingOccurrencesOfString:@"-----END CERTIFICATE-----" withString:@""];
}
NSData * cerdata = [[NSData alloc]initWithBase64EncodedData:[cerstr dataUsingEncoding:NSUTF8StringEncoding] options:NSDataBase64DecodingIgnoreUnknownCharacters];
SecCertificateRef rootcert =SecCertificateCreateWithData(kCFAllocatorDefault,CFBridgingRetain(cerdata));
const void *array[1] = { rootcert };
certs = CFArrayCreate(NULL, array, 1, &kCFTypeArrayCallBacks);
CFRelease(rootcert); // for completeness, really does not matter
}
SecTrustRef trust = [[challenge protectionSpace] serverTrust];
int err;
SecTrustResultType trustResult = 0;
err = SecTrustSetAnchorCertificates(trust, certs);
if (err == noErr) {
err = SecTrustEvaluate(trust,&trustResult);
}
// CFRelease(trust);
BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed)||(trustResult == kSecTrustResultConfirm) || (trustResult == kSecTrustResultUnspecified));
if (trusted) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}else{
[challenge.sender cancelAuthenticationChallenge:challenge];
}}
这是在测试情况下,一般权威的HTTPS 移动端是不许呀做处理的
网友评论