美文网首页
检测ios网络

检测ios网络

作者: usuer | 来源:发表于2019-07-15 17:42 被阅读0次

下载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;
    }
   
    
    
}

相关文章

网友评论

      本文标题:检测ios网络

      本文链接:https://www.haomeiwen.com/subject/duntlctx.html