Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Checking for an internet connection on mobile

Discussion in 'Delphi Android And IOS' started by AdminDF, May 15, 2014.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member DelphiFan Administrator Staff Member DF Staff

    UPDATE: The project has been updated to include retrieving the current SSID (that the device is connected to). For Android, this means adding the Access Wifi State permission. Refer to the updated text in the article.
    It’s a question that has been asked a few times in the Embarcadero forums: how to check if a mobile device (either Android or iOS) has an internet connection.
    I already knew of a solution for iOS, and recently I came across one for Android, so I figured I would combine the two and create a platform-independent unit, called NetworkState. In that unit, I created an abstract class that could be derived from in other units that would provide the implementation for each platform, i.e. Android and iOS. The declaration for the abstract class TCustomNetworkState looks like this:

    [hr]
    Code:
    TCustomNetworkState = class(TObject)
      function GetSSID: String; virtual; abstract;
      function IsConnected: Boolean; virtual; abstract;
      function IsWifiConnected: Boolean; virtual; abstract;
      function IsMobileConnected: Boolean; virtual; abstract;
    end;
    As can be seen, the intention is to simply provide functions for checking if either there is a connection at all, and specifically whether there is either a Wifi or Mobile (Cellular data) connection, and what the SSID is. It was also the intention that the implementation use the supported methods for checking for connections, rather than say, using the Indy components.
    The units NetworkState.iOS and NetworkState.Android each declare TPlatformNetworkState that provide the actual implementation, and the implementation uses clause of NetworkState is appropriately IFDEF’d for each platform so that the actual instance that is created is the one for the specific platform being compiled for.
    I’ve included a demo project below that includes all the necessary files. If you’re going to use TNetworkState in your own project, make sure the files:
    libReachability.a
    NetworkState.pas
    NetworkState.iOS.pas
    NetworkState.Android.pas
    SCNetworkReachability.pas
    CaptiveNetwork.pas
    are either in your project directory or in the compiler search path. Bear in mind that for the Android platform you’ll need to include the Access Network State, and Access Wifi State permissions in the project options:

    [​IMG]

    For iOS on the device, you’ll need to add the SystemConfiguration framework in the SDK manager. Please refer to this article, and instead of CoreBluetooth (as per the 2nd image), use SystemConfiguration.

    Download HERE
     
    asachanr likes this.
  2. pumidru
    Online

    pumidru DF Junior

    my Check Internet for IOS and Android

    Code:
    procedure TFormMain.BtnnMyCheckInternetClick(Sender: TObject);
      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;
    
    begin
      if CheckInternet = true then
      begin
        LocalConnect.enabled := true;
        DnsConnect.enabled := true;
        Button2.Visible := false;
      end
      else
      begin
        LocalConnect.enabled := false;
        DnsConnect.enabled := false;
        showmessage('Internet is not available!');
      end;
    end;
    
     
  3. perdcomp
    Offline

    perdcomp DF Junior

    thanks
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block