在全局检测移动网络的变化
2016-09-06 00:00
375 查看
导入Reachability.h类
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里添加
#pragma mark == 监听网络状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.internetReachability=[Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:_internetReachability];
AppDelegate.m里添加方法
- (void)reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
[self updateInterfaceWithReachability:curReach];
}
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
NetworkStatus netStatus = [reachability currentReachabilityStatus];
switch (netStatus) {
case 0:
{
//输出提示框
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前无网络" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
[alert show];
}
break;
case NotReachable:
NSLog(@"网络状态不可达");
break;
case ReachableViaWiFi:
NSLog(@"Wifi");
break;
case ReachableViaWWAN:
NSLog(@"3G");
break;
}
}
当网络状态发生变化时会调用该方法,无网络是弹出提示框
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里添加
#pragma mark == 监听网络状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.internetReachability=[Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:_internetReachability];
AppDelegate.m里添加方法
- (void)reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
[self updateInterfaceWithReachability:curReach];
}
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
NetworkStatus netStatus = [reachability currentReachabilityStatus];
switch (netStatus) {
case 0:
{
//输出提示框
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前无网络" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
[alert show];
}
break;
case NotReachable:
NSLog(@"网络状态不可达");
break;
case ReachableViaWiFi:
NSLog(@"Wifi");
break;
case ReachableViaWWAN:
NSLog(@"3G");
break;
}
}
当网络状态发生变化时会调用该方法,无网络是弹出提示框
相关文章推荐
- Android中检测手机制式和移动网络类型
- Android中检测手机制式和移动网络类型
- Android监听网络状态变化与WIFI移动网的判别
- Android 利用BroadcastReceiver实时检测网络状态变化
- Android:检测网络状态&监听网络变化
- 【CBM666 的全局检测鼠标移动】
- Android:检测网络状态&监听网络变化
- 检测网络变化(wifi、2g、3g、4g)
- reachbility检测网络变化
- 安卓检测设备网络变化
- 移动手机OMS检测TD网络情况
- Android中检测手机制式和移动网络类型
- Android中检测手机制式和移动网络类型
- Android中检测手机制式和移动网络类型
- JS和css实现检测移动设备方向的变化并判断横竖屏幕
- Android中检测手机制式和移动网络类型
- js/css 检测移动设备方向的变化 判断横竖屏幕
- ios检测网络变化
- iOS 中设置全局网络检测
- [Android新手学习笔记28]-Broadcast-例一广播检测网络变化