-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkState.iOS.pas
113 lines (94 loc) · 3.19 KB
/
NetworkState.iOS.pas
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
unit NetworkState.iOS;
interface
uses
NetworkState;
type
TPlatformNetworkState = class(TCustomNetworkState)
public
function CurrentSSID: string; override;
function IsConnected: Boolean; override;
function IsWifiConnected: Boolean; override;
function IsMobileConnected: Boolean; override;
end;
implementation
uses
Macapi.ObjectiveC, Macapi.CoreFoundation, iOSApi.CocoaTypes, iOSApi.Foundation,
SCNetworkReachability, CaptiveNetwork;
type
SCNetworkReachabilityFlags = UInt32;
TReachability = class;
Reachability = interface(NSObject)
['{B405394F-57B1-4FF1-83D9-8FBFA38FFD7B}']
function startNotifier: LongBool; cdecl;
procedure stopNotifier; cdecl;
function isReachable: LongBool; cdecl;
function isReachableViaWWAN: LongBool; cdecl;
function isReachableViaWiFi: LongBool; cdecl;
function isConnectionRequired: LongBool; cdecl;
function connectionRequired: LongBool; cdecl;
function isConnectionOnDemand: LongBool; cdecl;
function isInterventionRequired: LongBool; cdecl;
function currentReachabilityStatus: NSInteger; cdecl;
function reachabilityFlags: SCNetworkReachabilityFlags; cdecl;
function currentReachabilityString: NSString; cdecl;
function currentReachabilityFlags: NSString; cdecl;
end;
ReachabilityClass = interface(NSObjectClass)
['{39EC0490-2787-4BB9-95EA-77BB885BFD01}']
function reachabilityWithHostname(hostname: NSString): pointer; cdecl;
function reachabilityForInternetConnection: pointer; cdecl;
function reachabilityWithAddress: pointer; cdecl;
function reachabilityForLocalWiFi: pointer; cdecl;
end;
TReachability = class(TOCGenericImport<ReachabilityClass, Reachability>)
end;
{$IFDEF CPUARM}
function FakeLoader: Reachability; cdecl; external 'libReachability.a' name 'OBJC_CLASS_$_Reachability';
{$ENDIF}
function GetInternetReachability: Reachability;
begin
Result := TReachability.Wrap(TReachability.OCClass.reachabilityForInternetConnection);
end;
{ TPlatformNetworkState }
function TPlatformNetworkState.CurrentSSID: string;
var
IntfArray: CFArrayRef;
NetworkInfo: NSDictionary;
IntfName: CFStringRef;
SSID: Pointer;
begin
Result := '';
IntfArray := CNCopySupportedInterfaces;
if Assigned(IntfArray) then
begin
IntfName := CFStringRef(CFArrayGetValueAtIndex(IntfArray, 0));
if Assigned(IntfName) then
begin
NetworkInfo := TNSDictionary.Wrap(CNCopyCurrentNetworkInfo(IntfName));
if Assigned(NetworkInfo) then
begin
SSID := NetworkInfo.valueForKey(NSSTR('SSID'));
if Assigned(SSID) then
Result := UTF8ToString(TNSString.Wrap(SSID).UTF8String);
end;
end;
end;
end;
function TPlatformNetworkState.IsConnected: Boolean;
begin
Result := GetInternetReachability.isReachable;
end;
function TPlatformNetworkState.IsMobileConnected: Boolean;
begin
Result := GetInternetReachability.isReachableViaWWAN;
end;
function TPlatformNetworkState.IsWifiConnected: Boolean;
begin
Result := GetInternetReachability.isReachableViaWiFi;
end;
initialization
{$IFDEF CPUARM}
if False then
FakeLoader;
{$ENDIF}
end.