[iOS]iOS中网络请求判断是否设置代理

额,最近公司的接口让人给通过抓包给破解了,搞不清楚https的证书是怎么泄露的,然后就有了这么个需求,只要是网络设置了代理,那么就禁止一切网络访问,这里记录一下如何判断是否设置了代理。
需要导入框架CFNetwork
然后,这个方法是mrc的:需要添加-fno-objc-arc的flag
代码如下:


+ (BOOL)getProxyStatus {
    NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]);
    NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.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;
    }
}

你可能感兴趣的:([iOS]iOS中网络请求判断是否设置代理)