一、AFSecurityPolicy的使用
- (AFSecurityPolicy *)securityPolicy{
[NSMutableArray arrayWithCapacity:1];
// .crt --->.cer
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"https" ofType:@"cer"];
NSData *data = [NSData dataWithContentsOfFile:cerPath];
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);//打印不出來
NSSet *cerSet = [NSSet setWithObject:data];
AFSecurityPolicy *security = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:cerSet];
[AFSecurityPolicy defaultPolicy];
security.allowInvalidCertificates = YES;
security.validatesDomainName = NO;
return security;
}
二悍抑、解決第一次下載安裝app時(shí)鳄炉,無網(wǎng)絡(luò)的bug
if (__IPHONE_10_0) {
[self cellularData];
}else{
[self startMonitoringNetwork];
}
#pragma mark - 網(wǎng)絡(luò)權(quán)限監(jiān)控
- (void)cellularData{
CTCellularData *cellularData = [[CTCellularData alloc] init];
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
switch (state) {
case kCTCellularDataRestrictedStateUnknown:
NSLog(@"不明錯(cuò)誤.....");
break;
case kCTCellularDataRestricted:
NSLog(@"沒有授權(quán)....");
[self testBD]; // 默認(rèn)沒有授權(quán) ... 發(fā)起短小網(wǎng)絡(luò)彈出授權(quán)框
break;
case kCTCellularDataNotRestricted:
NSLog(@"授權(quán)了////");
[self startMonitoringNetwork];//授權(quán)之后在監(jiān)控網(wǎng)絡(luò)
break;
default:
break;
}
};
}
#pragma mark - startMonitoringNetwork
- (void)startMonitoringNetwork{
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知網(wǎng)絡(luò),請檢查互聯(lián)網(wǎng)");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"無網(wǎng)絡(luò),請檢查互聯(lián)網(wǎng)");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"連接蜂窩網(wǎng)絡(luò)");
[self testBD];
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi網(wǎng)絡(luò)");
[self testBD];
break;
default:
break;
}
}];
[manager startMonitoring];
}
#pragma mark - 網(wǎng)絡(luò)測試接口
- (void)testBD{
NSString *urlString = @"http://api.douban.com/v2/movie/top250";
NSDictionary *dic = @{@"start":@(1),
@"count":@(5)
};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:urlString parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"請求成功:%@---%@",task,responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"錯(cuò)誤提示:%@---%@",task,error);
}];
}