在WWDC 2016上,苹果向开发者传递一个消息,到2017年1月1日时App Store中所有应用必须启用App Transport Security应用程序安全传输协议,也是就https。不过在2016年年底又宣布延期,延期到什么时候不清楚。
公司后台给我证书文件有服务器的server.cer、客户端client.p12、client.cer以及证书密码。一开始我就当着单向验证来的,结果一直失败,崩溃。后面后台给我一篇博客见 双向验证
1.单向验证(只需用到server.cer)
2 双向验证(只需用到client.p12、client.cer、需要用到证书密码)
3. 遇到的问题(其实都不是问题,小白可以借鉴下)
单向验证
除了设置plist App Transport Security Settings之外,还需要设置证书模式,见下面代码
```
@property(nonatomic, strong) AFHTTPSessionManager *manager;
@property(nonatomic, copy) NSString *url;
@property(nonatomic, strong) NSURLSessionDataTask *dataTask;
- (void)viewDidLoad {
[super viewDidLoad];
self.manager = [AFHTTPSessionManager manager];
self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
self.manager.requestSerializer = [AFHTTPRequestSerializer serializer];
}
- (IBAction)postAfn:(id)sender {
self.url = @"https://api.egdtv.com:444/actualSnatch/Video.json";// @"https://api.egdtv.com:444/actualSnatch/QQ.js";
__weak __typeof(self)weakSelf = self;
[self.dataTask cancel];
self.dataTask = [self.manager GET:self.url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
weakSelf.getResult.text = @"get成功";
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
weakSelf.getResult.text = [error.userInfo objectForKey:@"NSLocalizedDescription"];
}];
}
+(AFSecurityPolicy *)customSecurityPolicy{
// 设置证书模式
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"server" ofType:@"cer"];
NSData *cerDat = [NSData dataWithContentsOfFile:cerPath];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//允许自检证书
securityPolicy.allowInvalidCertificates = YES;
//域名与服务器一致
securityPolicy.validatesDomainName = YES;
securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerDat, nil];
return securityPolicy;
}
双向验证
感谢博主:http://www.jb51.net/article/100586.htm
可以先睹博主的AFN3.0及以上:http://xiazai.jb51.net/201612/yuanma/New_AFNetworking_For_HTTPS-master_jb51.zip
遇到的问题
1. afnetworking 9825
2. The server “ xxx” did not accept the certificate
3. afn cancelled
4. unacceptable content-type: application/javascript 刚开始调试的时候后台返回的js,没有设置AFN接收格式