ios网络连接状态监测
2015-06-22 21:32
639 查看
Reachability是苹果封装的一个用于监测网络状态的类,同时还可以检测出连接网络的类型(无连接,WiFi,3G),非常的轻巧,易用。
下载路劲:
苹果官方网站:http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
github:https://github.com/tonymillion/Reachability
使用方法:
1.将解压出来的Reachability.h和Reachability.m添加到项目中。
2.添加SystemConfiguration.framework到项目中。
3.在要使用的地方把头文件Reachability.h import进来:
下面以在程序刚起来的时候(didFinishLaunchingWithOptions)使用为例:
//AppDelegate.m
@interface AppDelegate (){
Reachability *_netWorkDetect;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 网络变化时通过通知中心调用提示函数netWorkChanged
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkChanged:) name:kReachabilityChangedNotification object:nil];
//获取监测器对象
_netWorkDetect = [Reachability reachabilityForInternetConnection];
//_netWorkDetect = [Reachability reachabilityWithHostName:@"www.baidu.com"];
//开始监测
[_netWorkDetect stopNotifier];
return YES;
}
-(void) netWorkChanged:(NSNotification *)notify
{
Reachability *currentReach = [notify object];
//获取网络状态
// NotReachable 无连接
// ReachableViaWiFi
WiFi
// ReachableViaWWAN 3G等无线网络
NetworkStatus status = [currentReach currentReachabilityStatus];
//网络无连接时,弹出警告视图
if (status == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
}
下载路劲:
苹果官方网站:http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
github:https://github.com/tonymillion/Reachability
使用方法:
1.将解压出来的Reachability.h和Reachability.m添加到项目中。
2.添加SystemConfiguration.framework到项目中。
3.在要使用的地方把头文件Reachability.h import进来:
下面以在程序刚起来的时候(didFinishLaunchingWithOptions)使用为例:
//AppDelegate.m
@interface AppDelegate (){
Reachability *_netWorkDetect;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 网络变化时通过通知中心调用提示函数netWorkChanged
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkChanged:) name:kReachabilityChangedNotification object:nil];
//获取监测器对象
_netWorkDetect = [Reachability reachabilityForInternetConnection];
//_netWorkDetect = [Reachability reachabilityWithHostName:@"www.baidu.com"];
//开始监测
[_netWorkDetect stopNotifier];
return YES;
}
-(void) netWorkChanged:(NSNotification *)notify
{
Reachability *currentReach = [notify object];
//获取网络状态
// NotReachable 无连接
// ReachableViaWiFi
WiFi
// ReachableViaWWAN 3G等无线网络
NetworkStatus status = [currentReach currentReachabilityStatus];
//网络无连接时,弹出警告视图
if (status == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
}
相关文章推荐
- python socket之tcp介绍
- 进程间通信(11) - 网络套接字(socket)[2]
- TCP/IP协议族-----10、移动IP
- 网络流24题:运输问题
- 几种创建XMLHttpRequest对象的方法
- 网络知识
- wp8将List对象转Json字符串用于网络传输
- 网络设备配置与管理---VLAN间路由实现部门间通信
- 网络设备配置与管理----通过VLAN划分隔离各公司的网络
- LVS在大规模网络环境中的应用
- 网络设备配置与管理----调试Cisco Catalyst交换机
- 【转】关于TCP和UDP协议消息保护边界的介绍
- tcp异常断开的重连解决方法
- Unity+NGUI打造网络图片异步加载和本地缓存工具(一)
- 【转】TCP协议的无消息边界问题
- 黑马程序员--网络编程
- 各网络设备的冲突域和广播域
- 深入理解linux网络技术内幕读书笔记(八)--设备注册与初始化
- Access, Trunk和Hybrid三种vlan模式理解
- 《UNIX网络编程 卷1》之"学习环境搭建"(CentOS 7)