ios网络状态监听

来源:互联网 发布:foxpro数据库 编辑:程序博客网 时间:2024/05/16 04:48
//状态改变
- (void)reachabilityChanged:(NSNotification *)note
{
Reachability *curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
//改变后的处理
- (void)updateInterfaceWithReachability:(Reachability *)curReach
{
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == ReachableViaWWAN) {
NSLog(@"3g/2g");
} else if (status == ReachableViaWiFi) {
NSLog(@"wifi");
} else {
NSLog(@"no net!");
}
}
//给应用添加网络监听
- (void)addNetworkNotification
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
_isHostReach = [Reachability reachabilityWithHostName:@"www.apple.com"];
[_isHostReach startNotifier];
}

在入口方法中添加addNetworkNotification就可以了
0 0