Nearly 5 years ago I wrote an article for checking whether a mobile device has an internet connection. This article revisits that solution, dropping the “less-used” functionality, however it also gains an important feature.

The code and demo for this article was written and tested on Delphi Tokyo 10.2.3, however it should work on earlier versions. It relies on units found in the KastriFree project.

In the original article, the main function of the code was to test if the mobile device has an internet connection. This new solution does essentially the same, however it also has an event that is fired when the connectivity changes, i.e. if the internet connection is lost, or becomes connected.

The TConnectivity class has two class methods (i.e. you do not need an instance to call these methods), which are fairly self-explanatory:

IsConnectedToInternet
IsWifiInternetConnection

It also has an event handler called OnConnectivityChange, which is called when (as you may expect) the internet connectivity changes, passing a Boolean that indicates whether the connection was gained, or lost. On Android, this is achieved by using the MultiReceiver class from the KastriFree project, to receive broadcasts of connectivity changes.

There is also an important difference to the code from the original article regarding iOS: This solution no longer requires the libReachability library, thus resolving the compatibility issues people have been having recently. Many thanks to Horácio Filho for his help in making the Reachability APIs from the System Configuration framework work on Delphi. I may have been floundering for a while longer if it were not for his assistance.

Rather than go into a discussion of the code, I will field questions in the comments.