Check if device has connection during app run and how to handle if connection is lost

rozman50rozman50 Member ✭✭
edited December 3 in Xamarin.Forms

What way would be the best way to check for internet connection while user is using app?

On startup I'm checking for internet connection with Xamarin.Essentials Connectivity and it works.

But if phone looses connection when app is fetching data from the internet System.Exception "unable to resolve host ..." is thrown and app crashes.
How to intercept this exception and how to handle it? Ideally I'd like to show NoConnectionPage (which I show if device doesn't have internet connection on app startup) or show an alert.

This is how I send request

            string token = await SecureStorage.GetAsync("auth_token").ConfigureAwait(false);
            var request = new HttpRequestMessage(HttpMethod.Get, BaseAddress);
            request.Headers.Accept.Clear();
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

            var response = await _client.SendAsync(request, CancellationToken.None).ConfigureAwait(false);
            if (response.IsSuccessStatusCode)
            {
            ...

HttpRequestException doesn't catch this exception!

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    But a generic Exception should catch it
  • igorkr_10igorkr_10 Member ✭✭✭
    edited December 3

    Catch it like this:

    catch (HttpRequestException)
    {
        await App.MainPage.DisplayAlert("Error", "Connection failure", "OK");
    }
    
  • JoeMankeJoeManke USMember ✭✭✭✭✭

    I always check the internet connection immediately before sending every web request. If they lose connection in the middle of a request you should be able to catch some exception and handle it.

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited December 4

    Check if device has connection during app run

    You can create a viewmodel to handle the connection state changes by subscribing to the ConnectivityChanged event.

    public class BaseViewModel : INotifyPropertyChanged
    {
        public bool IsNotConnected { get; set; }
        public BaseViewModel()
        {
            Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
            IsNotConnected = Connectivity.NetworkAccess != NetworkAccess.Internet;
        }
    
        BaseViewModel()
        {
            Connectivity.ConnectivityChanged -= Connectivity_ConnectivityChanged;
        }
    
        void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
        {
            IsNotConnected = e.NetworkAccess != NetworkAccess.Internet;
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    Tutorial: https://xamgirl.com/handling-connection-changes-in-xamarin-forms/

  • rozman50rozman50 Member ✭✭

    @JoeManke
    What do you do after you check for connection and there is none?

    If I push NoConnectionPage to the stack then I must prevent back button press, so user can't go back.
    Setting MainPage = NoConnectionPage would be a solution, but takes a lot of resources and Navigation stack is lsot.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    I basically do what @igorkr_10 suggested. Instead of navigating away, display an alert that says "No network connection" and provide a retry/refresh button on the existing page.

Sign In or Register to comment.