IOS开发小记

1.对服务器进行Https请求 -服务器:获取证书后需提供文件***.pem文件给IOS开发
-IOS开发:将得到的pem文件双击加入钥匙串后导出一份cer
-项目:将对应的cer文件放置在文件中,并在网络请求的方法中添加以下代码

+ (AFSecurityPolicy *)customSecurityPolicy{
NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"www.yqb168.com" ofType:@"cer"];
NSData * cerData = https://www.it610.com/article/[NSData dataWithContentsOfFile:cerPath];
NSSet * cerSet = [NSSet setWithObjects:cerData, nil];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
// 是否验证域名
securityPolicy.validatesDomainName = YES;
// 信任非法证书(自签名证书)
securityPolicy.allowInvalidCertificates = NO;
// 设置证书
[securityPolicy setPinnedCertificates:cerSet];
return securityPolicy;
}
2.数据请求报错:The data couldn’t be read because it isn’t in the correct format. -问题1:后台返回数据格式有误;
-问题2:app请求参数数据序列化的格式(requestSerializer)不正确;
-问题3:其他。
后台及其他问题暂不考虑,IOS app端更改requestSerializer设置:AFHTTPRequestSerializer/AFJSONRequestSerializer
+ (AFHTTPSessionManager *)apiRequest:(AFHTTPRequestSerializer *)serializer{
serializer = [AFJSONRequestSerializer serializer];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFSecurityPolicy *securityPolicy = [self customSecurityPolicy]; //https设置,http可直接alloc后使用
[manager setSecurityPolicy:securityPolicy];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; //Very Important
serializer.timeoutInterval = 40.f;
manager.requestSerializer = serializer;
return manager;
}
【IOS开发小记】待续。

    推荐阅读