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

nicookly nicookly 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
使用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)
{
...
}

}
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
nicookly
iOS 29,038 成员 12,459 内容