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.
TCustomNetworkState =
class
(TObject)
 Â
function
GetSSID:
String
; virtual; abstract;
 Â
function
IsConnected:
Boolean
; virtual; abstract;
 Â
function
IsWifiConnected:
Boolean
; virtual; abstract;
 Â
function
IsMobileConnected:
Boolean
; virtual; abstract;
end
;
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):
function CheckInternet: 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.
Not working in Delphi XE7. Please do needful.
I’m using it in my app that I’ve brought from XE5 to XE6 and now to XE7. What error are you getting?
In “Uses” we need to add Androidapi.Helpers. I missed it. Now working fine. Thank you.
Sorry i responed bit early.
It was compiling the project but giving the segmentation error (11) at
NetworkState.Android.pas
function TPlatformNetworkState.IsConnected
“ActiveNetwork := ConnectivityManager.getActiveNetworkInfo”;
Please help.
Do you have Android internet permissions turned on?
Yes. For both “Access network state” and “Access wifi state” i have.
What device are you running it on?
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.
http://www.fmxexpress.com/easily-use-threads-in-delphi-xe5-firemonkey-on-android-and-ios/
By default for “Internet” the permission is true ?
Moto G (Android)
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.
Please attach some sample demo code for Delphi XE7 version. I will check where it is going wrong.
Good evening, How Can I Download the sources , because the site in question has not the Link to download.
Thank you!!