Is there any way (private method or otherwise) to acccess the global proxy settings on an iOS device?
We are experiencing issues with the global proxy, set through an MDM profile, not being respected by iOS. This seems to have started with a late version of iOS 9 and is continuing with iOS 10. I would like to write an app to test for the presences of a proxy PAC file and then test a list of URLs to see how they are processed by the PAC file. I know using NSURL and the high-level APIs are supposed to automatically use the global proxy, but I am trying to write this app for testing purposes.
I have written an app using CFNetworkCopySystemProxySettings to retrieve the proxy settings, but it returns an empty dictionary on my test iOS device, even though the global proxy is set. Any help would be greatly appreciated.
Sincerely,
Doug Penny
The problem here is that
CFNetworkCopySystemProxySettings
hasn’t been audited for Swift happiness, and so it returns an
Unmanaged<CFDictionary>
(you can tell this by option clicking on the
proxySettings
identifier). You’ll need to convert the unmanaged reference to a managed reference like so:
import Foundation
if let myUrl = URL(string: "http://www.apple.com") {
if let proxySettingsUnmanaged = CFNetworkCopySystemProxySettings() {
let proxySettings = proxySettingsUnmanaged.takeRetainedValue()
let proxiesUnmanaged = CFNetworkCopyProxiesForURL(myUrl as CFURL, proxySettings)
let proxies = proxiesUnmanaged.takeRetainedValue()
print(proxies)
}
}
In this case I’m using
takeRetainedValue()
because both
CFNetworkCopySystemProxySettings
and
CFNetworkCopyProxiesForURL
return a +1 reference count per the CF retain/release rules (they have
Copy or
Create in the name).
In real code it’s better to just wrap routines like this with a function that follows Swift conventions. For example:
import Foundation
func QCFNetworkCopySystemProxySettings() -> CFDictionary? {
guard let proxiesSettingsUnmanaged = CFNetworkCopySystemProxySettings() else {
return nil
}
return proxiesSettingsUnmanaged.takeRetainedValue()
}
func QCFNetworkCopyProxiesForURL(_ url: URL, _ proxiesSettings: CFDictionary) -> [[String:AnyObject]] {
let proxiesUnmanaged = CFNetworkCopyProxiesForURL(url as CFURL, proxiesSettings)
let proxies = proxiesUnmanaged.takeRetainedValue()
return proxies as! [[String:AnyObject]]
}
if let myUrl = URL(string: "http://www.apple.com") {
if let proxySettings = QCFNetworkCopySystemProxySettings() {
let proxies = QCFNetworkCopyProxiesForURL(myUrl, proxySettings)
print(proxies)
}
}
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"