I'm attempting to use the CFNetwork framework in iOS in order to retrieve the proxy settings, and have the following...

I'm attempting to use the CFNetwork framework in iOS in order to retrieve the proxy settings, and have the following code:

uses
Posix.Dlfcn, Macapi.CoreFoundation, Macapi.ObjectiveC;

const
libCFNetwork = '/System/Library/Frameworks/CFNetwork.framework/CFNetwork';

function CFNetworkCopySystemProxySettings: CFDictionaryRef; cdecl;
external libCFNetwork name _PU + 'CFNetworkCopySystemProxySettings';

var
_Module: THandle = 0;

function CFNetworkModule: THandle;
begin
if _Module = 0 then
_Module := dlopen(MarshaledAString(libCFNetwork), RTLD_LAZY);
Result := _Module;
end;

function CFNetworkConst(const Key: string): Pointer;
begin
Result := dlsym(CFNetworkModule, MarshaledAString(Key));
end;

function GetProxyInfo(var AProxy: string; var APort: Integer): Boolean;
var
LDictRef: CFDictionaryRef;
LProxyRef: CFStringRef;
LPortRef: CFNumberRef;
begin
Result := False;
LDictRef := CFNetworkCopySystemProxySettings;
if LDictRef <> nil then
try
LProxyRef := CFStringRef(CFDictionaryGetValue(LDictRef, CFNetworkConst('kCFNetworkProxiesHTTPProxy')));
if LProxyRef <> nil then
try
AProxy := CFStringRefToStr(LProxyRef);
finally
CFRelease(LProxyRef);
end;
LPortRef := CFNumberRef(CFDictionaryGetValue(LDictRef, CFNetworkConst('kCFNetworkProxiesHTTPPort')));
if LPortRef <> nil then
try
APort := TNSNumber.Wrap(LPortRef).intValue;
Result := True;
finally
CFRelease(LPortRef);
end;
finally
CFRelease(LDictRef);
end;
end;

The value for _Module is valid, but the code is failing to retrieve the CFNetwork constants (Result in CFNetworkConst is nil, for both).

Thanks in advance for any help

Comments