1、判断网络请求是否开启代理
先上代码:
- (BOOL)isProxyOpened {
NSDictionary *proxySettings = (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
NSLog(@"%@", proxySettings);
NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:@"http://www.baidu.com"]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));
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"]){
NSLog(@">>>>>>没有设置代理");
return NO;
}else{
NSLog(@">>>>>>设置了代理");
return YES;
}
}
复制这段代码就可以用了。
下面一起分学习下他的代码:
CFNetworkCopySystemProxySettings
/*!
@function CFNetworkCopySystemProxySettings
@discussion Returns a CFDictionary containing the current system internet proxy settings.
@result Returns a dictionary containing key-value pairs that represent
the current internet proxy settings. See below for definitions of the keys and
values.
NULL if no proxy settings have been defined or if an error
was encountered.
The caller is responsible for releasing the returned dictionary.
*/
CFN_EXPORT __nullable CFDictionaryRef
CFNetworkCopySystemProxySettings(void) CF_AVAILABLE(10_6, 2_0);
官方注释,大致意思是 返回一个包含当前系统Internet代理设置的 CFDictionary类型的字典。
因为是C类型的字典 所以用__bridge
桥接,强转为OC的NSDictionary 类型。
添加下面代码,分别输出代理未开启和开启状态下的字典:
NSLog(@"%@", proxySettings);
未开启:

开启:

可以看出,代理开启状态,字典包含代理服务器、端口、状态等信息。
CFNetworkCopyProxiesForURL
/*
* CFNetworkCopyProxiesForURL()
*
* Discussion:
* Given a URL and a proxy dictionary, determines the ordered list
* of proxies that should be used to download the given URL.
*
* Parameters:
*
* url:
* The URL to be accessed
*
* proxySettings:
* A dictionary describing the available proxy settings; the
* dictionary's format should match the dictionary returned
* by CFNetworkCopySystemProxySettings described below.
*
* Result:
* An array of dictionaries; each dictionary describes a single
* proxy. See the comment at the top of this file for how to
* interpret the returned dictionaries.
*
*/
CFN_EXPORT CFArrayRef
CFNetworkCopyProxiesForURL(CFURLRef url, CFDictionaryRef proxySettings) CF_AVAILABLE(10_5, 2_0);
官方注释给出的大致意思是此方法会根据我们传入的URL和代理设置字典,确定应该用于下载给定URL的代理的有序列表。
有序列表是一个字典的数组,每一个字典都描述一个代理。
添加代码输出这个数组:
NSLog(@"%@", proxies);
输出结果:

取出其中kCFProxyTypeKey
对应的值,即为现在代理的状态,当值为@"kCFProxyTypeNone"
,则表示无代理。
PS:实际使用可少第一步C->OC 的字典转换
2、判断VPN的连接。
上代码:
- (BOOL)isVPNConnected {
NSDictionary * proxySettings = (__bridge NSDictionary *)CFNetworkCopySystemProxySettings();
NSLog(@"%@", proxySettings);
NSArray * keys = [proxySettings[@"__SCOPED__"] allKeys];
for (NSString * key in keys) {
if ([key rangeOfString:@"tap"].location != NSNotFound ||
[key rangeOfString:@"tun"].location != NSNotFound ||
[key rangeOfString:@"ppp"].location != NSNotFound) {
NSLog(@">>>>>>开启了VPN");
return YES;
}
}
NSLog(@">>>>>>没有开启VPN");
return NO;
}
和判断代理相似,不同的是判断VPN链接,这里需要取出字典proxySettings
对应@"__SCOPED__"
的key的值。值同样是字典。
判断最终字典的所有key是否包含tap
、tun
、 ppp
字段。三个字段和VPN功能相关。
代码输出结果为:

网友评论