Check Network Connectivity State With Delphi XE5 Firemonkey On Android And IOS

If you are looking for a cross platform way to check for connectivity in your Delphi Firemonkey apps on Android and IOS then this might be the solution for you. It is a simple set of free classes that lets you do just that. For IOS it requires a small static library be included as well. It has properties to report back on if there is any connectivity at all and whether that connectivity is via Wifi or Mobile data. Personally I found it pretty useful because it is cross platform and will tell you the connectivity regardless if it is Android or IOS. It doesn’t report connectivity on Windows so you’ll have to add that part yourself with IFDEFs.

Delphi/Pascal

1

2

<div>

<div><code>TCustomNetworkState=

Delphi/Pascal

1

class

Delphi/Pascal

1

(TObject)

Delphi/Pascal

1

Delphi/Pascal

1

function

Delphi/Pascal

1

GetSSID:

Delphi/Pascal

1

String

Delphi/Pascal

1

;virtual;abstract;

Delphi/Pascal

1

Delphi/Pascal

1

function

Delphi/Pascal

1

IsConnected:

Delphi/Pascal

1

Boolean

Delphi/Pascal

1

;virtual;abstract;

Delphi/Pascal

1

Delphi/Pascal

1

function

Delphi/Pascal

1

IsWifiConnected:

Delphi/Pascal

1

Boolean

Delphi/Pascal

1

;virtual;abstract;

Delphi/Pascal

1

Delphi/Pascal

1

function

Delphi/Pascal

1

IsMobileConnected:

Delphi/Pascal

1

Boolean

Delphi/Pascal

1

;virtual;abstract;

Delphi/Pascal

1

end

Delphi/Pascal

1

;

You can also use some code like this from Daniel Magin as a backup if you need to (put your own domain place of google.com):

Delphi/Pascal

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

functionCheckInternet:boolean;

begin

result:=false;

try

IdTCPClient1.ReadTimeout:=2000;

IdTCPClient1.ConnectTimeout:=2000;

IdTCPClient1.Port:=80;

IdTCPClient1.Host:=’google.com’;

IdTCPClient1.Connect;

IdTCPClient1.Disconnect;

result:=true;

except

result:=false;

end;

end;

Update: Apparently on IOS you should never do synchronous network calls (including this check connectivity functionality) in the main thread. We’ve run into problems with this on iPad Air devices. One recommendation is to place this connectivity check in an anonymous background thread.

I think your error may have to do with if you are calling it from the main thread. I have a boolean property on my form called Connected so I can just read it whenever. Then I have an Anonymous method called CheckNetworkThread; which does the NetworkState.IsConnected and sets my form Connected boolean. I call CheckNetworkThread (which is non-blocking) and then I just check my Connected variable.

In XE6 i have called it deirectly on main thread only. There it is working fine. Where as in XE7 it is not.
I have tried it in Anonymous thread also as suggected by you. Once it is trying to call NetworkState.IsConnected it is giving access violation error. I believe some thing is going wrong in “NetworkState.Android”, but unable to trace it.