0%

macOS获取系统代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL);
if (proxies) {
NSDictionary *dict = [NSDictionary dictionaryWithDictionary:(__bridge NSDictionary *)proxies];
CFNumberRef hpEnableRef = CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPEnable);
NSNumber *enable = (__bridge NSNumber *)hpEnableRef;
if (enable) {
// host
CFStringRef hpURLRef = (CFStringRef)CFDictionaryGetValue(proxies,kSCPropNetProxiesHTTPProxy);
// port
CFNumberRef hpPortRef = CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPPort);

if (hpURLRef) {
NSNumber *port = (__bridge NSNumber *)hpPortRef;
NSString *url = (__bridge NSString *)hpURLRef;
NSString *pUrl = [NSString stringWithFormat:@"http://%@:%@",url,port];
self.proxyServer = pUrl;
}
}
CFRelease(proxies);
}

参考