fytoahse
fytoahse

Reputation: 145

Check async connection to internet with Xam.Plugin.Connectivity

I am trying to verify if my application has access to the internet, for this I occupy the Plugin Xam.Plugin.Connectivity in its version 4 ... The problem is that the answer I receive when Pinging a web is always false

This is the function that I call from the Login of my App ... as you can see it is an async function ...

        private async void Login()
        {
            IsRunning = true;
            IsEnabled = false;

            var connection = await apiService.CheckConnection();
            if (!connection.IsSuccess)
            {
                IsRunning = false;
                IsEnabled = true;

                await dialogService.ShowMessage("Error", connection.Message);
                return;
            }

}

and this is the class to verify the state of the connection

public async Task<Response> CheckConnection()
        {   
            if (!CrossConnectivity.Current.IsConnected)
            { 
                    return new Response
                    {
                        IsSuccess = false,
                        Message = "Porfavor active su servicio de internet."
                    };

                }

        var isReachable = await CrossConnectivity.Current.IsRemoteReachable("www.google.com");

                if (!isReachable)
                {

                return new Response
                {
                    IsSuccess = false,
                    Message = "Verifique su conexion a internet."
                };

                }

                else
                {
                    return new Response
                    {
                        IsSuccess = true,
                        Message = "OK"
                    };

                }

            }

the problem occurs when it reaches the IsRecheable variable, it takes the false value and does not enter the if it follows ...

enter image description here

but it returns the state of the "null" connection immediately ...

enter image description here

and I'm also getting the following error

enter image description here

I have given the corresponding permissions to run the plugin

what's going on? Any call async that I'm doing wrong? any recommendation with this plugin? some help?

Upvotes: 0

Views: 781

Answers (1)

SushiHangover
SushiHangover

Reputation: 74184

www.google.com is not a properly formatted URL as it is missing the protocol (i.e. http).

http://www.google.com would be a proper formatted URL that can be parsed into all of its URI components.

Example:

var isReachable = await CrossConnectivity.Current.IsRemoteReachable("http://www.google.com");

Note: IsRemoteReachable also can accept just a host name, so google.com or stackoverflow.com would also work.

Upvotes: 2

Related Questions