/, General tips/Connectivity checker for mobile, revisited

Connectivity checker for mobile, revisited

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.

By |2018-07-15T15:07:05+00:00July 15, 2018 3:07 pm|Code tips, General tips|6 Comments

About the Author:

6 Comments

  1. Erick Suarez July 15, 2018 at 3:24 pm - Reply

    Nice!!! Testing!

  2. Pieter Bas Hofstede September 14, 2018 at 6:14 pm - Reply

    Thanks a lot, just noticed the old solution took 1,5sec on recent iOS devices so had to make the switch to the new solution 🙂

  3. Kevin October 2, 2018 at 3:30 pm - Reply

    Dave,

    Your Connectivity demo fails with Tokyo 10.3.2

    I have a parallels VM connected via my Mac. I pulled the plug on my router and connected via the iPhone hotspot. In the VM I can bring up a Chrome window and browse to wherever. Your demo says internet not connected where it clearly is.

    Any ideas?

    Kevin

  4. kevin October 2, 2018 at 4:00 pm - Reply

    Dave,

    And just to be clear, that’s a build for Win32. So MBP with Parallels V13 and Win 10 VM with Delphi 10.2.3.

    The same happens if I connect to my Router (not the iPhone Hotspot). I am connected from my parallels VM through the mac and, even though there is a robust connection (23Mbs download and 9Mbs upload) the demo returns NOT connected to internet. I have this code ion my application which also returns not connected to internet:

    function IsInternetConnected: Boolean;
    var
    FConnectivity: TConnectivity;
    sMsgConnect: string;



    Result := True;
    FConnectivity := TConnectivity.Create;
    if FConnectivity.IsConnectedToInternet then
    begin
    sMsgConnect := ‘Connected to the internet’;
    if FConnectivity.IsWifiInternetConnection then
    sMsgConnect := sMsgConnect + ‘ via Wifi’;
    WriteLog(‘INFO ‘, sMsgConnect);
    end
    else
    raise Exception.Create(‘Not connected to internet’);

    except

    Result := False;
    end;

    • Dave October 2, 2018 at 4:02 pm - Reply

      The connectivity checker is for mobile, not for Windows

  5. Kevin October 2, 2018 at 4:37 pm - Reply

    Explains a lot, thanks, when I get to iOS / Android I’ll give it a go.

Leave a Reply

Show Buttons
Hide Buttons