判断网络状态不准确,请大神帮忙看看为什么?

nicookly 2015-01-06 04:07:30
使用Reachability 3.5 ,测试时候先断开网络,当下判定status 为NotReachable,而当连接网络后status 的返回值为:NotReachable ,具体代码如下:请大神们 帮帮忙看下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//开启网络状况的监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;

self.isReachable = YES;
switch ([self.hostReach currentReachabilityStatus]) {
case NotReachable:
self.isReachable = NO;
//NSLog(@"notReachable");
break;
case ReachableViaWiFi:
self.isReachable = YES;
//NSLog(@"WIFI");
break;
case ReachableViaWWAN:
self.isReachable = YES;
//NSLog(@"3G");
break;
}
//开始监听,会启动一个run loop
[self.hostReach startNotifier];

return YES;
}

-(void)reachabilityChanged:(NSNotification *)note
{
self.isFirstReachable = YES;
Reachability *currReach = [note object];
NSParameterAssert([currReach isKindOfClass:[Reachability class]]);

//对连接改变做出响应处理动作
NetworkStatus status = [currReach currentReachabilityStatus];

//如果没有连接到网络就弹出提醒实况

if(status == NotReachable)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];

self.isReachable = NO;

return;
}
if(status == ReachableViaWiFi || status == ReachableViaWWAN)
{
...
}

}
...全文
139 点赞 收藏 1
写回复
1 条回复
Bannings 2015年01月06日
这个其实是正常的,你可以看看苹果官方的例子。输出如下:

2015-01-06 16:38:05.732 Reachability[1587:331315] Reachability Flag Status: WR tc----- localWiFiStatusForFlags
2015-01-06 16:38:05.753 Reachability[1587:331315] Reachability Flag Status: WR tc----- networkStatusForFlags
2015-01-06 16:38:05.820 Reachability[1587:331315] Reachability Flag Status: WR tc----- networkStatusForFlags
2015-01-06 16:38:05.822 Reachability[1587:331315] Reachability Flag Status: WR tc----- networkStatusForFlags
2015-01-06 16:38:05.904 Reachability[1587:331315] Reachability Flag Status: -R ------d localWiFiStatusForFlags
2015-01-06 16:38:05.946 Reachability[1587:331315] Reachability Flag Status: -- ------- networkStatusForFlags
2015-01-06 16:38:05.951 Reachability[1587:331315] Reachability Flag Status: -- ------- networkStatusForFlags
2015-01-06 16:38:05.953 Reachability[1587:331315] Reachability Flag Status: -- ------- networkStatusForFlags
2015-01-06 16:38:06.125 Reachability[1587:331315] Reachability Flag Status: -R -----l- networkStatusForFlags
2015-01-06 16:38:06.197 Reachability[1587:331315] Reachability Flag Status: -R ------- networkStatusForFlags
2015-01-06 16:38:06.199 Reachability[1587:331315] Reachability Flag Status: -R ------- networkStatusForFlags
2015-01-06 16:38:14.558 Reachability[1587:331315] Reachability Flag Status: WR t------ localWiFiStatusForFlags
2015-01-06 16:38:14.589 Reachability[1587:331315] Reachability Flag Status: WR t----l- networkStatusForFlags
2015-01-06 16:38:14.605 Reachability[1587:331315] Reachability Flag Status: WR t------ networkStatusForFlags
2015-01-06 16:38:14.607 Reachability[1587:331315] Reachability Flag Status: WR t------ networkStatusForFlags
2015-01-06 16:38:17.408 Reachability[1587:331315] Reachability Flag Status: -R ------d localWiFiStatusForFlags
2015-01-06 16:38:17.443 Reachability[1587:331315] Reachability Flag Status: -R -----l- networkStatusForFlags
2015-01-06 16:38:17.464 Reachability[1587:331315] Reachability Flag Status: -R ------- networkStatusForFlags
2015-01-06 16:38:17.466 Reachability[1587:331315] Reachability Flag Status: -R ------- networkStatusForFlags
我也是先开飞行模式进入,再打开网络,同你的输出是一样的,可以看到网络连通后有了一次短暂的断开,我认为这是手机先通过运营商连接了网络,然后短暂的断开,连接了WiFi。
回复 点赞
发动态
发帖子
iOS
创建于2008-03-10

1.4w+

社区成员

1.2w+

社区内容

主要讨论与iOS相关的软件和技术
社区公告
暂无公告