93 网络状态监测
2015-09-06 11:56
561 查看
需要工程导入SystemConfiguration.framework #import "Reachability.h" - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //网络改变会发出通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil]; //获得对象 self.conn = [Reachability reachabilityForInternetConnection]; //开始使之能发出通知 [self.conn startNotifier]; } - (void)dealloc { [self.conn stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)networkStateChange { [self checkNetworkState]; } // 用WIFI // [wifi currentReachabilityStatus] != NotReachable // [conn currentReachabilityStatus] != NotReachable // 没有用WIFI, 只用了手机网络 // [wifi currentReachabilityStatus] == NotReachable // [conn currentReachabilityStatus] != NotReachable // 没有网络 // [wifi currentReachabilityStatus] == NotReachable // [conn currentReachabilityStatus] == NotReachable - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { } - (void)checkNetworkState { // 1.检测wifi状态 Reachability *wifi = [Reachability reachabilityForLocalWiFi]; // 2.检测手机是否能上网络(WIFI\3G\2.5G) Reachability *conn = [Reachability reachabilityForInternetConnection]; // 3.判断网络状态 if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi NSLog(@"有wifi"); } else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网 NSLog(@"使用手机自带网络进行上网"); } else { // 没有网络 NSLog(@"没有网络"); } }
相关文章推荐
- 客户端与服务器进行数据传输时的TCP窗口与拥塞处理
- 网络相关命令
- 我是菜鸟:网络知识回顾
- HTTP HTTPs
- SOAPUI系列13- SOAPUI 测试Http 协义
- overlay网络技术之VXLAN详解
- HDU 3549 Flow Problem(网络流入门题-最大流的Ford-Fulkerson算法)
- 网络分析软件和linux下网络性能测试软件iperf
- serviceMix https搭建
- 常见HTTP状态码
- file_operation(文件操作) file(文件) inode(节点) http://blog.itpub.net/14805538/viewspace-445624/
- TCP/IP(3)IP
- 大端法、小端法、网络字节序
- Linux学习记录(9)Linux基本网络命令
- tcp连接 三次握手+四次挥手
- android中判断网络是否可用
- Windows 7/XP 局域网络共享文件详细设置教程
- 深入浅出HTTP协议
- C语言中socket相关网络编程函数小结
- 网络流增广路入门很好的文章