借助于 CFNetwork 在应用从后台进入前台时检测是否设置代理,若是设置代理进行弹框提示web
+ (BOOL)getProxyStatus {
NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]);
NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.baidu.com"], (CFDictionaryRef)proxySettings) autorelease]);
NSDictionary *settings = [proxies objectAtIndex:0];
NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);
NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);
NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);
if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"])
{
//没有设置代理
return NO;
}
else
{
//设置代理了
return YES;
}
}
复制代码
另外,对于autorelease,若是项目设置了ARC,能够在Target-》Build Phase-》Compile Source中将相应的非ARC文件,
Compiler Flag改成-fno-objc-arc便可。
复制代码
**考虑证书有效期的话就设置不一样的 pinning mode **安全
SSL Pinning,即SSL证书绑定。经过SSL证书绑定来验证服务器身份,防止应用被抓包。服务器
客户端须要证书(Certification file), .cer格式的文件。能够跟服务器端索取。 若是他们给个.pem文件,要使用命令行转换:markdown
openssl x509 -inform PEM -in name.pem -outform DER -out name.cer
复制代码
若是给了个.crt文件,请这样转换:网络
openssl x509 -in name.crt -out name.cer -outform der
复制代码
若是啥都不给你,你只能本身动手了:ui
openssl s_client -connect www.website.com:443 </dev/null 2>/dev/null | openssl x509 -outform DER > myWebsite.cer**
复制代码
把生成的.cer证书文件直接拖到你项目的相关文件夹中,记得勾选Copy items if neede和Add to targets。spa
AFSecurityPolicy SSLPinningMode AFSecurityPolicy是AFNetworking中网络通讯安全策略模块。它提供三种SSL Pinning Mode ]命令行
AFSSLPinningModeNone:彻底信任服务器证书;
AFSSLPinningModePublicKey:只比对服务器证书和本地证书的Public Key是否一致,若是一致则信任服务器证书;
AFSSLPinningModeCertificate:比对服务器证书和本地证书的全部内容,彻底一致则信任服务器证书;
复制代码
选择那种模式呢?代理
AFSSLPinningModeCertificate:最安全的比对模式。可是也比较麻烦,由于证书是打包在APP中,若是服务器证书改变或者到期,旧版本没法使用了,咱们就须要用户更新APP来使用最新的证书。
AFSSLPinningModePublicKey:只比对证书的Public Key,只要Public Key没有改变,证书的其余变更都不会影响使用。
若是你不能保证你的用户老是使用你的APP的最新版本,因此咱们使用AFSSLPinningModePublicKey。
复制代码
+ (AFHTTPSessionManager *)manager
{
static AFHTTPSessionManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey withPinnedCertificates:[AFSecurityPolicy certificatesInBundle:[NSBundle mainBundle]]];
manager.securityPolicy = securityPolicy;
});
return manager;
}
复制代码