iOS学习之--------------使用系统方法检测网络情况

来源:互联网 发布:用手机写小说的软件 编辑:程序博客网 时间:2024/05/22 07:46

可以使用系统自带的方法对程序的网络情况进行检测,具体实现代码如下:

@property (nonatomic,strong)Reachability * conn

1、创建监听
self.conn = [[NSNotificationCenterdefaultCenter]addObserver : selfselector @selector(check)name:kReachabilityChangedNotificationobject:nil];

self.conn=[ReachabilityreachabilityForInternetConnection];
//开始监控
[self.connstartNotifier];
- (void)dealloc
{
     //结束监控
    [self.connstopNotifier];
     //移除
    [[NSNotificationCenterdefaultCenter]removeObserver:self];
}


- (void)check
{
   
//检测wifi状态
   
Reachability *wifi = [ReachabilityreachabilityForLocalWiFi];
   
//检测手机自带的上网功能
   
Reachability *conn = [ReachabilityreachabilityForInternetConnection];
   
if ([wifi currentReachabilityStatus] !=NotReachable) {
       
NSLog(@"wife");
    }
else if ([conn currentReachabilityStatus]){
       
NSLog(@"使用手机的3g网络");
    }
else {
       
NSLog(@"没有网络");
    }
}


0 0
原创粉丝点击