iOS 监控网络状态

苹果官方提供 Reachability 检测网络状态

https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

 

添加框架SystemConfiguration.framework

 2 
 3  - (void)viewDidLoad
 4  {
 5       [super viewDidLoad];
 6       
 7       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];}
1 - (void)dealloc
2  {
3 4      [[NSNotificationCenter defaultCenter] removeObserver:self];
5  }
 1 - (void)checkNetworkState
 2 {
 3      // 1.检测wifi状态
 4      Reachability *wifi = [Reachability reachabilityForLocalWiFi];
 5      
 6      // 2.检测网络(WIFI\3G\2.5G)
 7      Reachability *conn = [Reachability reachabilityForInternetConnection];
 8      
 9      // 3.判断网络状态
10      if ([wifi currentReachabilityStatus] != NotReachable) { // wifi
11          12          
13      } else if ([conn currentReachabilityStatus] != NotReachable) { // 手机网络
14         15          
16      } else { // 没有网络
17          
18          19      }
20  }

 

posted on 2015-01-23 13:39  airy99  阅读(192)  评论(0编辑  收藏  举报