-2

如果 google.com 不起作用!但是网络还可以。代码会带来错误。所以我想删除“google.com”来检测 net 的状态。

我找不到没有地址的可达性方法。

#import "Reachability.h"

static  ReachabilityCenter *reachCenter;
@implementation ReachabilityCenter

@synthesize reachablity;
@synthesize isConnectedNet;

+(id)shareCenter
{
    if(!reachCenter)
    {
         reachCenter = [[ReachabilityCenter alloc] init];
         reachCenter.reachablity = [Reachability reachabilityWithHostname:@"www.google.com"];
    }
    return reachCenter;
}

-(void)reachabilityChanged:(NSNotification*)note
{
    Reachability * reach = [note object];
    isConnectedNet = [reach isReachable];
}

-(void)beginListening
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];

    [reachCenter.reachablity  startNotifier];

}
@end

我测试代码检测没有网站,ip 的状态。

-(void)detectNetStatus
{
    Reachability *r =  [Reachability reachabilityForLocalWiFi];
    isConnectedNet = [r currentReachabilityStatus];
}

-(NetworkStatus)isConnectedNet
{
    [self detectNetStatus];
    return isConnectedNet;
}
4

1 回答 1

0

您要问的实际上是不可能的,原因如下:您无法在不使用网络连接的情况下“肯定”(以您想要的方式)验证网络连接是否处于活动状态。为了使用它,您必须将它用于意味着与预期资源 X、Y 或 Z 的连接。

例如,考虑一下:如果您连接到没有外部 Internet 访问权限的本地网络怎么办?看起来连接是可用的,实际上它确实 - 这不是你的想法。

因此,您必须针对 X、Y 或 Z 进行测试,以验证您想要访问的内容是否可用。

添加信息:

您可以确定网络连接是否可用:

寻找:可达性* internetReach;

在 Apple 的示例中:http: //developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_ReachabilityAppDelegate_h.html#//apple_ref/doc/uid/DTS40007324-Classes_ReachabilityAppDelegate_h-DontLinkElementID_3

The problem is --- just because a network connection is available, and the above internetReach will --- it doesn't tell you anything about the connection. It could just be a local network. You can't find out until you try to connect something specific you expect to be available like, say, Apple.com or Google.com

于 2013-01-14T03:21:17.450 回答