iphone上的蜂窝网络的互联网连接返回false(xamarin.ios)

时间:2013-12-07 13:31:52

标签: ios xamarin.ios monodevelop xamarin internet-connection

以下代码对于wifi连接返回true,但在设备上检查蜂窝(wwan)网络时返回false 这是代码

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;
}

我收到错误

  

远程服务器返回错误:(403)禁止

帮忙。

2 个答案:

答案 0 :(得分:3)

查看Xamarin Reachability类here

编辑:

http://db.tt/SqQGQ9Ci

下载并安装vodafone个人资料

答案 1 :(得分:0)

您可以使用可达性

#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;
    }
}

}