Detect Network Connectivity and TimeZone changes in Xamarin Forms

This article describes how to detect the network connectivity status and time zone changes in Xamarin forms in all the three (Windows, Android & ios) platforms.

This article is in continuation to previous article – MVVM in Xamarin Forms UWP 10, implementing these changes in MVVM pattern.

Detect Network Connectivity:

In general, network is a platform specific component and requires the platform specific code changes in all the platforms. But in Xamarin forms, there is cross connectivity plugin which will take care of all these platform specific implementations. Hence write the code in Xamarin forms project and run in all the platforms.  Install the Xam.Plugin.Connectivity plugin from NuGet   in to Xamarin forms project and it’s platform specific projects

Use below code to get the network status whether connected or disconnected.

    public bool IsNetworkAvailable()
        {
            return CrossConnectivity.Current.IsConnected;
        }

If you want to alert the user with connectivity changes when app is active, use the ConnectivityChanged event as below.

  public NetworkService()
        {
            // registering event for network connectivity changes
            CrossConnectivity.Current.ConnectivityChanged += this.NetworkConnectivityChanged;
        }
  private async void NetworkConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
        {
            await App.Current.MainPage.DisplayAlert("Alert", message, "OK");  
       }

In case, if you need to get the SSID of the network that your device gets connected, use platform specific implementation as below.

Create an interface in Xamarin forms project

public interface IPlatformService
    {
        string GetSSID();
    }

Create a class in UWP project and register with dependency

[assembly: Xamarin.Forms.Dependency(typeof(PlatformService))]
namespace XamarinFormsIOC.UWP.DependencyServices
{
    public class PlatformService : IPlatformService
    {
         public string GetSSID()
        {
            var icp = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
            if (icp != null)
            {
                 return icp.WlanConnectionProfileDetails.GetConnectedSsid();
             }

             return null;
        }
    }
}

Now, in Xamarin forms project use dependencyService to access the platform specific SSID function to get the SSID for device connected network as below.

DependencyService.Get<IPlatformService>().GetSSID();

Use below code to detect the network ConnectionType that the device is connected

          var connectionType = CrossConnectivity.Current.ConnectionTypes.FirstOrDefault();
             if (connectionType == ConnectionType.WiFi)
             {
             }

Detect TimeZone:

In case, if you are working with DateTime’s for examples messaging app where you might need to refresh the messages datetime in appropriate to timezone changes when app is active or in background.

We don’t have any direct mechanism or approach to refresh the timezone in latest versions of .Net Framework. Use below work around and then use DateTime.

//refresh the timezone when app is in background(user manually changes the timezone in device)
TimeZoneInfo.ConvertTime(new DateTime(0), TimeZoneInfo.Utc);
var dt = DateTime.Now;

Download the working sample.

      

By Siva Jagan Dhulipalla   Popularity  (4167 Views)