发布于 

iOS: 手机是否设置网络代理

情景

项目开发过程中, 会遇到下面这样一种情景:

产品或者设计同事需要体验目前产品开发的进度, 然后要求开发人员, 将现在的版本给他们体验.
做过程序开发的人都知道, 开发版本会有不少问题.
这时, 产品和设计同事就会抱怨, 怎么登录不了, 这里没有数据了!
有个小伙伴被折腾的实在受不了, 连网页都打不开了.
最后找到原因:
他手机被之前的程序猿设置了网络代理, 代理到程序猿哥哥的电脑上来抓包调试问题, 忘记取消设置了.

假如可以判断当前手机是否设置了网络代理, 就可以很好地解决上面情景中提到的问题了.

找到线索

在 ASIHttpRequest 中找到如下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- (BOOL)configureProxies
{
// Have details of the proxy been set on this request
if (![self isPACFileRequest] && (![self proxyHost] && ![self proxyPort])) {
// If not, we need to figure out what they'll be
NSArray *proxies = nil;
// Have we been given a proxy auto config file?
if ([self PACurl]) {
// If yes, we'll need to fetch the PAC file asynchronously, so we stop this request to wait until we have the proxy details.
[self fetchPACFile];
return NO;
// Detect proxy settings and apply them
} else {
#if TARGET_OS_IPHONE
NSDictionary *proxySettings = [NSMakeCollectable(CFNetworkCopySystemProxySettings()) autorelease];
#else
NSDictionary *proxySettings = [NSMakeCollectable(SCDynamicStoreCopyProxies(NULL)) autorelease];
#endif
proxies = [NSMakeCollectable(CFNetworkCopyProxiesForURL((CFURLRef)[self url], (CFDictionaryRef)proxySettings)) autorelease];
// Now check to see if the proxy settings contained a PAC url, we need to run the script to get the real list of proxies if so
NSDictionary *settings = [proxies objectAtIndex:0];
if ([settings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]) {
[self setPACurl:[settings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]];
[self fetchPACFile];
return NO;
}
}
if (!proxies) {
[self setReadStream:nil];
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to obtain information on proxy servers needed for request",NSLocalizedDescriptionKey,nil]]];
return NO;
}
// I don't really understand why the dictionary returned by CFNetworkCopyProxiesForURL uses different key names from CFNetworkCopySystemProxySettings/SCDynamicStoreCopyProxies
// and why its key names are documented while those we actually need to use don't seem to be (passing the kCF* keys doesn't seem to work)
if ([proxies count] > 0) {
NSDictionary *settings = [proxies objectAtIndex:0];
[self setProxyHost:[settings objectForKey:(NSString *)kCFProxyHostNameKey]];
[self setProxyPort:[[settings objectForKey:(NSString *)kCFProxyPortNumberKey] intValue]];
[self setProxyType:[settings objectForKey:(NSString *)kCFProxyTypeKey]];
}
}
return YES;
}

踏破铁鞋无觅处…

判断 iphone 是否设置网络代理的示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+ (BOOL)hasSetProxy
{
BOOL proxy = NO;

NSDictionary *proxySettings = (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef)(url),
(__bridge CFDictionaryRef)(proxySettings)));
MZLOG(@"proxies:%@", proxies);
NSDictionary *settings = proxies[0];
MZLOG(@"kCFProxyHostNameKey: %@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);
MZLOG(@"kCFProxyPortNumberKey: %@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);
MZLOG(@"kCFProxyTypeKey: %@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);
if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"]) {
proxy = NO;
}
else {
proxy = YES;
}

return proxy;
}

用户体验

判断如果设置了代理, 提示用户, 帮他直接跳到 wifi 设置界面.

下面说说如何跳到系统的 wifi 设置界面.

1.配置 URL TYPES

1

注意这里的配置是 prefs.

2.openURL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSString * urlString = @"prefs:root=WIFI";
NSURL *url = [NSURL URLWithString:urlString];
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:[NSURL URLWithString:urlString]]) {

if ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0) {

[app openURL:url options:@{} completionHandler:nil];
}
else {

[app openURL:url];
}
}

另外, 跳转到系统其他界面的 URL 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1.系统设置
prefs:root=INTERNET_TETHERING
2.WIFI设置
prefs:root=WIFI
3.蓝牙设置
prefs:root=Bluetooth
4.系统通知
prefs:root=NOTIFICATIONS_ID
5.通用设置
prefs:root=General
6.显示设置
prefs:root=DISPLAY&BRIGHTNESS
7.壁纸设置
prefs:root=Wallpaper
8.声音设置
prefs:root=Sounds
9.隐私设置
prefs:root=privacy
10.打开 APP Store
prefs:root=STORE
11.打开 Notes
prefs:root=NOTES
12.打开 Safari
prefs:root=Safari
13.打开 Music
prefs:root=MUSIC
14.打开 photo
prefs:root=Photos

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

veryitman