IOS开发小记
1.对服务器进行Https请求
-服务器:获取证书后需提供文件***.pem文件给IOS开发
-IOS开发:将得到的pem文件双击加入钥匙串后导出一份cer
-项目:将对应的cer文件放置在文件中,并在网络请求的方法中添加以下代码
+ (AFSecurityPolicy *)customSecurityPolicy{2.数据请求报错:The data couldn’t be read because it isn’t in the correct format. -问题1:后台返回数据格式有误;
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:app请求参数数据序列化的格式(requestSerializer)不正确;
-问题3:其他。
后台及其他问题暂不考虑,IOS app端更改requestSerializer设置:AFHTTPRequestSerializer/AFJSONRequestSerializer
+ (AFHTTPSessionManager *)apiRequest:(AFHTTPRequestSerializer *)serializer{【IOS开发小记】待续。
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;
}
推荐阅读
- 深入理解Go之generate
- 2020-04-07vue中Axios的封装和API接口的管理
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 私通和背叛,他怎么看(——晨读小记)
- iOS中的Block
- 日更小小记
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础