检测ios网络

2019-07-15  本文已影响0人  usuer

下载Reachability.h和Reachability.m文件

#import "AppDelegate.h"
#import "Reachability.h"
@interface AppDelegate ()
@property(nonatomic,strong)Reachability *reachability;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(internetConnection) name:kReachabilityChangedNotification object:nil];
       //初始化可达性
    self.reachability =[Reachability reachabilityForInternetConnection];
    //开始监听
    [self.reachability startNotifier];
 
    [self internetConnection];
  }
-(void)internetConnection
{   //网络连接现状
    NetworkStatus status =[self.reachability currentReachabilityStatus];
    switch (status) {
        case NotReachable:
            
            NSLog(@"没有网络");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"netStatus" object:nil userInfo:@{@"netType":@"notReachable"}];
            
            break;
        case ReachableViaWiFi:
            
            NSLog(@"wifi连接");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"netStatus" object:nil userInfo:@{@"netType":@"reachableViaWiFi"}];

            
            break;
        case ReachableViaWWAN:
            
            NSLog(@"移动蜂窝网络");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"netStatus" object:nil userInfo:@{@"netType":@"reachableViaWWAN"}];

            
            break;
        default:
            break;
    }
   
    
    
}
上一篇 下一篇

猜你喜欢

热点阅读