网络监控

使用官网Reachability库经行网络实时监控

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 2 {
 3     //开启网络状况的监听
 4     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
 5 
 6     self.hostReach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
 7     [self.hostReach startNotifier];  //开始监听,会启动一个run loop
 8 }
 9 
10 
11 - (void) reachabilityChanged: (NSNotification*)notice {
12     Reachability * reach = [notice object];
13     
14     if(![reach isReachable])
15     {
16        NSLog( @"网络不可用");
17         return;
18     } 
19     if (reach.isReachableViaWiFi) {
20         NSLog( @"当前通过wifi连接");
21         return;
22     }
23     if (reach.isReachableViaWWAN) {
24        NSLog(  @"当前通过手机网络连接");
25         return;
26     }
27 }
原文地址:https://www.cnblogs.com/mapanguan/p/5455567.html