Internet connectivity returns false for cellular networks on iphone(xamarin.ios)

2.2k views Asked by At

The following code returns true for wifi connection but false while checking for cellular(wwan) network on device , here is the code

try
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
    request.Timeout = 25000;
    request.Credentials = CredentialCache.DefaultNetworkCredentials;
    request.UseDefaultCredentials=true;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    return response.StatusCode == HttpStatusCode.OK;
} 
catch (Exception e)
{
    return false;
}

i am getting the error as

The remote server returned an error: (403) Forbidden

help out.

2

There are 2 answers

3
Chethan Shetty On BEST ANSWER

Check out Xamarin Reachability class here.

Edit:

Download and install the vodafone profile from http://db.tt/SqQGQ9Ci

1
Kirtikumar A. On

You can use Reachibility

#import "Reachability.h"
+(bool)internetConnection
 {

Reachability* reachability;
reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
[reachability startNotifier];
switch (netStatus)
{
    case NotReachable:
    {
        //[self showLoadingView:@"Internet Unavailable!!"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NetworkFail" object:self];
        return NO;
        break;
    }

    case ReachableViaWWAN:
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NEtworkPass" object:self];
        return YES;
        break;
    }
    case ReachableViaWiFi:
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NEtworkPass" object:self];
        return YES;
        break;
    }
}

}