IOS网络——检测网络状态:Reachability

1.首先下载Reachability包

2.导入Reachability.h和Reachability.m文件到要开发的项目中去

3.加入SystemConfiguration.framework框架

4.编写代码

- (void)viewDidLoad {

    [super viewDidLoad];

    //注册通知

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(Changed:) name:kReachabilityChangedNotification object:nil];

    //创建监测类对象

    Reachability * reachability=[Reachability reachabilityWithHostName:@"www.beidu.com"];

    //判断没有网络

    if(reachability.currentReachabilityStatus==NotReachable){

        

        UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:@"友情提示" message:@"没有网路" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

        [alertView show];

    }

    //判断有无线网络

    if(reachability.currentReachabilityStatus==ReachableViaWiFi){

    

        UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:@"友情提示" message:@"有WiFi网络!" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

        [alertView show];

    }

    //判断有蜂窝网络

    if(reachability.currentReachabilityStatus==ReachableViaWWAN){

 

        UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:@"友情提示" message:@"有蜂窝网络!" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

        [alertView show];

    }

 

}

 

 

-(void)Changed:(Reachability *) sender

{

    NSLog(@"网路变化了!");

}

 

posted @ 2017-03-03 14:08  荆超  阅读(317)  评论(0编辑  收藏  举报