Check out Xamarin Reachability class here.
Edit:
Download and install the vodafone profile from http://db.tt/SqQGQ9Ci
题
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.
解决方案
Check out Xamarin Reachability class here.
Edit:
Download and install the vodafone profile from http://db.tt/SqQGQ9Ci
其他提示
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;
}
}
}