iOS监测网络状态

1.通过AFNetworking的AFNetworkReachabilityManager对象

监听网络方法

- (void)netWorkMonitor{
    manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                [ProgressHUD showSuccess:@"未识别的网络" Interaction:YES];
                break;
            case AFNetworkReachabilityStatusNotReachable:
                [ProgressHUD showSuccess:@"网络不可用,请打开WiFi或者移动蜂窝网络" Interaction:YES];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                [ProgressHUD showError:@"2G,3G,4G...的网络" Interaction:YES];
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                [ProgressHUD showSuccess:@"wifi的网络" Interaction:YES];
                break;
            default:
                break;
        }
    }];
    
    [manager startMonitoring];
}

调用

  [self performSelector:@selector(netWorkMonitor) withObject:nil afterDelay:0.2];
2.Reachability对象
  • 必须将 Reachability.h 和 Reachability.m 拷贝到工程中。
  • 导入#import "Reachability.h"
//监听部分
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
// 让Reachability对象开启被监听状态
[self.reach startNotifier];

//网络状态改变,弹出alertview提示框。
- (void)reachabilityChanged:(NSNotification *)note
{
    // 通过通知对象获取被监听的Reachability对象
    Reachability *curReach = [note object];
    
    // 获取Reachability对象的网络状态
    NetworkStatus status = [curReach currentReachabilityStatus];
    
    if (status == NotReachable)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"不能访问网络,请检查网络" delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil];
        [alert show];
    }
}

注意:使用Reachability发通知的时候,在模拟器中网络状态改变reachabilityChanged一直会被调用两次,查了好久不知道什么原因。这种情况不知道只有我碰到了还是?

  • Reachability写法2
-(void)checkInternet
{
    self.internetReachability = [Reachability reachabilityForInternetConnection];
    if (self.internetReachability.currentReachabilityStatus==NotReachable)
    {
        NSLog(@"网络不可用...");
        [self internetAlertView];
    } else{
        NSLog(@"网络可用...");
    }
    [self.internetReachability startNotifier];
}

-(void)internetAlertView
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不能访问网络,请检查网络!" delegate:self cancelButtonTitle:@"OK"  otherButtonTitles:nil];
    [alertView show];
}

Reachability的使用,苹果官方demo地址

3.使用netdb.h
  • 必须导入如下头文件

include

include

+ 在Appdelegate.h文件中

-(BOOL)checkInternetConnection;


+ 在Appdelegate.m文件中 

-(BOOL)checkInternetConnection {

char *hostname;
struct hostent *hostinfo;
hostname = "baidu.com";
hostinfo = gethostbyname (hostname);
if (hostinfo == NULL)
{
    NSLog(@"-> no connection!\n");
    return NO;
}
else{
    NSLog(@"-> connection established!\n");
    return YES;
}

}

你可能感兴趣的:(iOS监测网络状态)