我正在考虑向我的iphone svn client添加代理支持。在iphone设置中设置系统范围的VPN时,可以添加全局代理。外部应用程序是否可以通过api读取此信息?
答案 0 :(得分:6)
Apple已为此创建了一个示例应用程序,名为CFProxySupportTool。
CFProxySupportTool显示了如何使用CFProxySupport API来确定网络连接是否应通过代理;如果您不使用Apple的高级网络API(如CFNetwork和Foundation URL加载系统),但仍希望解释系统提供的代理设置,则此功能非常有用。
目前可在 https://developer.apple.com/library/mac/#samplecode/CFProxySupportTool/Introduction/Intro.html
代码并不简洁(超过1000行),并且用简单的C编写。您还可以查看ASIHTTPRequest的startRequest函数的源代码,该函数似乎基于CFProxySupportTool。
这是一个开始:
systemProxySettings = [(NSDictionary *) CFNetworkCopySystemProxySettings() autorelease];
proxies = [(NSArray *) CFNetworkCopyProxiesForURL((CFURLRef) URL, (CFDictionaryRef) systemProxySettings) autorelease];
if (!proxies.count)
return;
firstProxySettings = [proxies objectAtIndex:0];
if (nil != (pacScriptURL = [firstProxySettings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]))
{
CFErrorRef cfErrorRef = NULL;
NSError *nsError = nil;
NSString *script;
script = [NSString stringWithContentsOfURL:pacScriptURL
usedEncoding:NULL
error:&nsError];
if (nsError)
return;
proxies = [(NSArray *) CFNetworkCopyProxiesForAutoConfigurationScript((CFStringRef) script, (CFURLRef) URL, &cfErrorRef) autorelease];
if (cfErrorRef || !proxies.count)
return;
firstProxySettings = [proxies objectAtIndex:0];
}
/* Now use `firstProxySettings' */
答案 1 :(得分:2)
Swift 4版本(特别感谢mortehu提供的初始示例)。
//Shown this way for clarity, you may not want to waste cycles in your production code
if let url = URL(string: "https://someurloutthere.com") {
let systemProxySettings = CFNetworkCopySystemProxySettings()?.takeUnretainedValue() ?? [:] as CFDictionary
let proxiesForTargetUrl = CFNetworkCopyProxiesForURL(url as CFURL, systemProxySettings).takeUnretainedValue() as? [[AnyHashable: Any]] ?? []
for proxy in proxiesForTargetUrl {
print("Proxy: \(String(describing: proxy))")
//Print the proxy type
print("Proxy Type: \(String(describing: proxy[kCFProxyTypeKey]))")
//There different proxy value keys depending on the type, this is an example of getting the proxy config script if the type is kCFProxyTypeAutoConfigurationURL. If the proxy type were kCFProxyTypeSOCKS you would want to access the SOCKS property keys to see/get the SOCKS proxy values
print("Proxy Autoconfig script URL: \(String(describing: proxy[kCFProxyAutoConfigurationURLKey]))"
}
}
答案 2 :(得分:0)
您是否使用ASIHttpRequest之类的内容进行了调查,请参阅描述代理支持的how to document部分。
至少源代码应包含一些有用的指导。
答案 3 :(得分:0)
查看CFProxySupport API,特别是CFNetworkCopyProxiesForURL()将让您阅读到达特定网址所需的代理。