Reachability实时监控网络变化

Reachability是一种实时观察网络发生变化控件,如当你的手机处于WiFi情况下,他就会检测环境,当处于GPS的情况下改变环境,当处于无网络的情况下又是一种环境。下面我们看下关于Reachability的用法、

1.用到Reachability的情况下,首先引入SystemConfiguration;

Reachability.h:

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>

typedef enum : NSInteger {
    NotReachable = 0,
    ReachableViaWiFi,
    ReachableViaWWAN
} NetworkStatus;

extern NSString *kReachabilityChangedNotification;

@interface Reachability : NSObject

+ (instancetype)reachabilityWithHostName:(NSString *)hostName;

+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;

+ (instancetype)reachabilityForInternetConnection;

+ (instancetype)reachabilityForLocalWiFi;

- (BOOL)startNotifier;
- (void)stopNotifier;

- (NetworkStatus)currentReachabilityStatus;

Reachability.m:

#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#import <sys/socket.h>

#import <CoreFoundation/CoreFoundation.h>

#import "Reachability.h"

NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotification";

#pragma mark - Supporting functions

#define kShouldPrintReachabilityFlags 1

static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment)
{
#if kShouldPrintReachabilityFlags

NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n",
          (flags & kSCNetworkReachabilityFlagsIsWWAN)                ? ‘W‘ : ‘-‘,
          (flags & kSCNetworkReachabilityFlagsReachable)            ? ‘R‘ : ‘-‘,

(flags & kSCNetworkReachabilityFlagsTransientConnection)  ? ‘t‘ : ‘-‘,
          (flags & kSCNetworkReachabilityFlagsConnectionRequired)   ? ‘c‘ : ‘-‘,
          (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)  ? ‘C‘ : ‘-‘,
          (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? ‘i‘ : ‘-‘,
          (flags & kSCNetworkReachabilityFlagsConnectionOnDemand)   ? ‘D‘ : ‘-‘,
          (flags & kSCNetworkReachabilityFlagsIsLocalAddress)       ? ‘l‘ : ‘-‘,
          (flags & kSCNetworkReachabilityFlagsIsDirect)             ? ‘d‘ : ‘-‘,
          comment
          );
#endif
}

static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
    NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
    NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");

Reachability* noteObject = (__bridge Reachability *)info;
    // Post a notification to notify the client that the network reachability changed.
    [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
}

#pragma mark - Reachability implementation

@implementation Reachability
{
    BOOL _alwaysReturnLocalWiFiStatus; //default is NO
    SCNetworkReachabilityRef _reachabilityRef;
}

+ (instancetype)reachabilityWithHostName:(NSString *)hostName
{
    Reachability* returnValue = NULL;
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
    if (reachability != NULL)
    {
        returnValue= [[self alloc] init];
        if (returnValue != NULL)
        {
            returnValue->_reachabilityRef = reachability;
            returnValue->_alwaysReturnLocalWiFiStatus = NO;
        }
    }
    return returnValue;
}

+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress
{
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress);

Reachability* returnValue = NULL;

if (reachability != NULL)
    {
        returnValue = [[self alloc] init];
        if (returnValue != NULL)
        {
            returnValue->_reachabilityRef = reachability;
            returnValue->_alwaysReturnLocalWiFiStatus = NO;
        }
    }
    return returnValue;
}

+ (instancetype)reachabilityForInternetConnection
{
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    
    return [self reachabilityWithAddress:&zeroAddress];
}

+ (instancetype)reachabilityForLocalWiFi
{
    struct sockaddr_in localWifiAddress;
    bzero(&localWifiAddress, sizeof(localWifiAddress));
    localWifiAddress.sin_len = sizeof(localWifiAddress);
    localWifiAddress.sin_family = AF_INET;

// IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0.
    localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);

Reachability* returnValue = [self reachabilityWithAddress: &localWifiAddress];
    if (returnValue != NULL)
    {
        returnValue->_alwaysReturnLocalWiFiStatus = YES;
    }
    
    return returnValue;
}

#pragma mark - Start and stop notifier

- (BOOL)startNotifier
{
    BOOL returnValue = NO;
    SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};

if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context))
    {
        if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
        {
            returnValue = YES;
        }
    }
    
    return returnValue;
}

- (void)stopNotifier
{
    if (_reachabilityRef != NULL)
    {
        SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    }
}

- (void)dealloc
{
    [self stopNotifier];
    if (_reachabilityRef != NULL)
    {
        CFRelease(_reachabilityRef);
    }
}

#pragma mark - Network Flag Handling

- (NetworkStatus)localWiFiStatusForFlags:(SCNetworkReachabilityFlags)flags
{
    PrintReachabilityFlags(flags, "localWiFiStatusForFlags");
    NetworkStatus returnValue = NotReachable;

if ((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect))
    {
        returnValue = ReachableViaWiFi;
    }
    
    return returnValue;
}

- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
{
    PrintReachabilityFlags(flags, "networkStatusForFlags");
    if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
    {
        // The target host is not reachable.
        
    
        return NotReachable;
    }

NetworkStatus returnValue = NotReachable;

if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
    {
        /*
         If the target host is reachable and no connection is required then we‘ll assume (for now) that you‘re on Wi-Fi...
         */
        returnValue = ReachableViaWiFi;
    }

if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
        (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
    {
        /*
         ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
         */

if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
        {
            /*
             ... and no [user] intervention is needed...
             */
           
            returnValue = ReachableViaWiFi;
        }
    }

if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
    {
        /*
         ... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
         */
        
        returnValue = ReachableViaWWAN;
    }
    
    return returnValue;
}

- (BOOL)connectionRequired
{
    NSAssert(_reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef");
    SCNetworkReachabilityFlags flags;

if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
    {
        return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
    }

return NO;
}

- (NetworkStatus)currentReachabilityStatus
{
    NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef");
    NetworkStatus returnValue = NotReachable;
    SCNetworkReachabilityFlags flags;
    
    if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
    {
   
        
        if (_alwaysReturnLocalWiFiStatus)
        {
            returnValue = [self localWiFiStatusForFlags:flags];
            
         
            
        }
        else
        {
            returnValue = [self networkStatusForFlags:flags];
            
            
        }
    }
    
    return returnValue;
}

@end

以上只是第三方组件,加之一些个人对其封装的一些方法,下面我门就要具体道每个Controller上了,来检测网络环境发生的变化

Controller.h

#import <UIKit/UIKit.h>
#import "Reachability.h"

@interface ReachViewController : UIViewController
- (void) reachabilityChanged:(NSNotification *)note;
- (void)updateInterfaceWithReachability:(Reachability *)reachability;
- (void)configureTextField:(UITextField *)textField imageView:(UIImageView *)imageView reachability:(Reachability *)reachability;

@end

Controller.m

#import "ReachViewController.h"
#import "MBProgressHUD.h"
#import "HUD.h"
@interface ReachViewController ()
@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;

@end

@implementation ReachViewController

- (void)viewDidLoad {
    [super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    NSString *remoteHostName = @"www.apple.com";
    NSString *remoteHostLabelFormatString = NSLocalizedString(@"Remote Host: %@", @"Remote host label format string");

self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
    [self.hostReachability startNotifier];
    [self updateInterfaceWithReachability:self.hostReachability];

}

- (void) reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self updateInterfaceWithReachability:curReach];
}

- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
    
    NetworkStatus netStatus = [reachability currentReachabilityStatus];

switch (netStatus) {
        case ReachableViaWWAN:
        {
            [HUD promptWithString:@"正在使用流量,建议用WiFi" onView:self.view];

}
            break;
        case ReachableViaWiFi:
        {
            [HUD promptWithString:@"WiFi已连接" onView:self.view];
        
        }
            break;
        default:
            break;
    }
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
当你网络环境发生变化的时候你就可以实时观察到。代码没有任何bug,新建demo,复制粘贴就可以看下效果,如果你的开发环境出现错误就是版本问题了。

时间: 2024-10-22 18:38:43

Reachability实时监控网络变化的相关文章

IOS 监控网络变化

随着移动网络升级:2G->3G->4G甚至相传正在研发的5G,网络速度是越来越快,但这流量也像流水一般哗哗的溜走. 网上不是流传一个段子: 睡觉忘记关流量,第二天房子就归移动了! 这固然是一种夸张的说法,但是作为一个优秀的App开发人员,我们需要在App中给出最恰当的提示. 在需要花费大量流量的时候(例如:视频,下载等)给出一些提示. 在用户视频期间用户切换网络的时候给出是否继续观看的提示! 下面笔者就给出一种可以实时监控网络状态改变的方法! 笔者使用的是网络流传最广的网络监控类:Reacha

iOS使用Reachability实时检测网络连接状况

//在程序的启动处,开启通知 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //..... //开启网络状况的监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kR

iOS实时监控网络状态的改变

在网络应用中,有的时候需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验 WIFI网络:自动下载高清图片 4G/3G网络:只下载缩略图 没有网络:只显示离线的缓存数据 常用的有以下两种方法: (1).使用苹果观法提供的检测iOS设备网络环境用的库 Reachablity (2).使用AFN框架中的AFNetworkReachabilityManager来监听网络状态的

用c#监控网络状态

1.查询当前网络状态: using Microsoft.VisualBasic.Devices; //判断当前网络连接状态 Network nw=new Network(); if(nw.IsAvailable) lbmsg.Text="网络已连接"; else lbmsg.Text="网络已断开"; 2.实时监控网络状态 using System.Net.NetworkInformation; // Constructor public MainPage() {

实时监控-CPU

实时监控-CPU:          mpstat:最大的特点是:可以查看多核cpu中每个计算核心的统计数据.语法                             mpstat [-P{|ALL}] [internal [count]]          mpstat参数: –P {|ALL} 表示监控哪个CPU,cpu在[0,cpu个数-1]中取值 Internal 相邻的两次采样的间隔时间 Count 采样的次数,count只能和delay一起使用 mpstat需要注意: 当没有参数

java利用WatchService实时监控某个目录下的文件变化并按行解析(注:附源代码)

首先说下需求:通过ftp上传约定格式的文件到服务器指定目录下,应用程序能实时监控该目录下文件变化,如果上传的文件格式符合要求,将将按照每一行读取解析再写入到数据库,解析完之后再将文件改名. 一. 一开始的思路 设置一个定时任务,每隔一分钟读取下指定目录下的文件变化,如果有满足格式的文件,就进行解析. 这种方式很繁琐,而且效率低,效率都消耗在了遍历.保存状态.对比状态上了! 而且无法利用OS的很多功能. 二. WatchService介绍 1. 该类的对象就是操作系统原生的文件系统监控器!我们都知

Java 文件变化加载监控,实时监控文件加载之Commons-io

Java 文件监控,实时监控文件加载 ,Commons-io中已有此功能的实现,如下代码所示: package wy.util.filemonitor; import java.io.File; import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; import org.apache.commons.io.monitor.FileAlterationObserver; import org.apache.log4j

搭建属于你的家庭网络实时监控–HTML5在嵌入式系统中的应用&#183;高级篇

*本文已刊登在<无线电>2014年第6期 <搭建属于你的在线实时采集系统>中已经对HTML5平台有了初步的认识,并基于此向大家展示了如何将采集到的数据上传至网络,实现实时观测.HTML 5是近十年来Web开发标准最巨大的飞跃.想必你已经体会到了HTML5的便捷之处,为了帮助大家更好在此平台上拓展应用,本文更加深入的探析"智能家居"与HTML5的完美结合,让家庭网络实时监控轻松实现. 关于智能家居  2013年,太多公司包括三星和飞利浦在内都对智能家居动了念头,在

ntopng网络流量实时监控

High-Speed Web-based Traffic Analysis and Flow Collection ntopng is the next generation version of the original ntop, a network traffic probe that monitors network usage. ntopng is based on libpcapand it has been written in a portable way in order to