美文网首页
iOS监测网络状态

iOS监测网络状态

作者: 天天想念 | 来源:发表于2016-02-26 18:02 被阅读394次
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<unistd.h>

include<netdb.h>

+ 在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中监测网络状态有好几种方式,详情点击这里。但是在一般的网络监测都只是能够监测本地的网络状态,而真实的网络可...

  • iOS监测网络状态

    .#### - 可以利用AFNetworking实现监测手机网络状态的功能 创建监听管理者 开始进行监控 判断当前...

  • iOS监测网络状态

    1.通过AFNetworking的AFNetworkReachabilityManager对象 监听网络方法 调用...

  • iOS 有关WIFI讯息获取

    iOS 有关WIFI讯息获取 流量监测 获取wifi地址 获取wifi名称 从手机状态栏获取当前网络状态

  • AFNetworking3.0网络状态监测<二>

    SystemConfiguration网络监测 网络状态监测使用SystemConfiguration这个API ...

  • iOS进阶之网络--网络状态监测

    Reachability简介 很多应用中都会涉及对用户手机所在网络环境(数据、WiFi、无网络)的监测,在不同的网...

  • iOS 实时监测网络状态

    最近公司需求APP在无网络下提示用户所以整理下自己写的代码,方便自己查阅以及给大家带来一点思路。

  • ios中网络状态的监测

    一:苹果官方提供的Reachability示例程序的监测 导入Reachability.h和Reachabilit...

  • iOS开发 监测网络状态

    一、说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一...

  • IOS开发笔记-监测网络状态

    两种方法监测网络状态: AFNetworking中AFNetworkReachabilityManager监测网络...

网友评论

      本文标题:iOS监测网络状态

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