官方Demo下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

将Reachability.h 和 Reachability.m 加到自己的项目中,并引用 SystemConfiguration.framework,就可以使用了。

下面代码:

//
// ViewController.m
// 网络状态监测
//
// Created by 王卫亮 on 15/2/4.
// Copyright © 2015年 王卫亮. All rights reserved.
// #import "ViewController.h"
#import "Reachability.h"
#import <ifaddrs.h> @interface ViewController ()
@property (nonatomic, strong)Reachability *conn;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 设置网络状态变化时的通知函数
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil]; self.conn = [Reachability reachabilityForInternetConnection];
[self.conn startNotifier];//开始监测
} -(void)dealloc{
[self.conn stopNotifier];//停止监测
[[NSNotificationCenter defaultCenter] removeObserver:self];
} - (void)networkStateChange{
[self checkNetworkState];
} //检测网络状态
- (void)checkNetworkState{
// 1.检测wifi状态
Reachability *wifi = [Reachability reachabilityForLocalWiFi]; // 2.检测手机是否能上网络(WIFI\3G\2.5G)
Reachability *conn = [Reachability reachabilityForInternetConnection]; // 3.判断网络状态
if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi
NSLog(@"有wifi");
} else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网
NSLog(@"使用手机自带网络进行上网");
} else { // 没有网络
NSLog(@"没有网络");
}
} //如果只是判断是否有网,可以返回BOOL值 - (BOOL)checkNetwork{
// 1.检测wifi状态
Reachability *wifi = [Reachability reachabilityForLocalWiFi]; // 2.检测手机是否能上网络(WIFI\3G\2.5G)
Reachability *conn = [Reachability reachabilityForInternetConnection]; // 3.判断网络状态
if ([wifi currentReachabilityStatus] != NotReachable || [conn currentReachabilityStatus] != NotReachable) {
return YES;
} else { // 没有网络
return NO;
}
} //需要导入ifadds头文件 //是否连接VPN - (BOOL)isVPNConnected{
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = ;
//检索当前接口,0 成功
success = getifaddrs(&interfaces);
if (success == ) {
//循环链表接口
temp_addr = interfaces;
while (temp_addr != NULL) {
NSString *string = [NSString stringWithFormat:@"%s" , temp_addr->ifa_name];
if ([string rangeOfString:@"tap"].location != NSNotFound ||
[string rangeOfString:@"tun"].location != NSNotFound ||
[string rangeOfString:@"ppp"].location != NSNotFound){
return YES;
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return NO;
} @end

很多公司的项目中都使用了AFNetworking, 或者封装了AFNetworking的YTKNetworking ,AFNetworking封装有Reachability的网络监测,可以直接拿来用

#pragma 监测网络的可链接性
+ (BOOL)netWorkReachabilityWithURLString:(NSString *) strUrl {
__block BOOL netState = NO;
NSURL *baseURL = [NSURL URLWithString:strUrl];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO];
netState = YES;
break;
case AFNetworkReachabilityStatusNotReachable:
netState = NO;
default:
[operationQueue setSuspended:YES];
break;
}
}]; [manager.reachabilityManager startMonitoring];
return netState;
}

iOS检测网络连接状态的更多相关文章

  1. iOS开发 - Swift实现检测网络连接状态及网络类型

    一.前言 在移动开发中,检测网络的连接状态尤其检测网络的类型尤为重要.本文将介绍在iOS开发中,如何使用Swift检测网络连接状态及网络类型(移动网络.Wifi). 二.如何实现 Reachabili ...

  2. [Swift通天遁地]四、网络和线程-(6)检测网络连接状态

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. Delphi检测网络连接状态

    有时候,我们做一些小软件就需要检测网络连接状态,比如想给你的软件加上类似QQ那样的系统消息,可是像我这样的穷人肯定是买不起服务器了,那我们只好另想办法,可以读取网页然后用浏览器显示,这个时候就需要判断 ...

  4. Android 检测网络连接状态

    Android连接网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置. 首先,要判断网络状态,需要有相应的权限,下面为权限代码(Androi ...

  5. android检测网络连接状态示例讲解

    网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置   Android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(Andro ...

  6. iOS 判断网络连接状态的几种方法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #801b80 } p.p2 ...

  7. qt检测网络连接状态【只能检测和路由器的连接,不能测试到外网的连接】

    #include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...

  8. Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面

    随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityManager  类,用 ...

  9. Android编程 获取网络连接状态 及调用网络配置界面

    获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...

随机推荐

  1. ifconfig报错:SIOCSIFFLAGS: Operation not permitted

    # insmod mt7601Usta.ko rtusb init rt2870 --->usbcore: registered new interface driver rt2870 # iw ...

  2. 解决pdm打开只显示表名不显示字段的步骤

    解决pdm打开只显示表名不显示字段的方法 选中PDM 依次点击 工具-->显示参数选择-->content 下面的table ,右边勾选上columns 点击OK 选择 all symbo ...

  3. ArrayList源码

      1.首先看对ArrayList的定义:   public class ArrayList<E> extends AbstractList<E> implements Lis ...

  4. [Java A] – is not an enclosing class

    public class A {public class B { }}; 需要实例B类时,按照正逻辑是,A.B ab = new A.B();那么编译器就会出现一个错误–“is not an encl ...

  5. POJ 4046 Sightseeing 枚举+最短路 好题

    有n个节点的m条无向边的图,节点编号为1~n 然后有点权和边权,给出q个询问,每一个询问给出2点u,v 输出u,v的最短距离 这里的最短距离规定为: u到v的路径的所有边权+u到v路径上最大的一个点权 ...

  6. jsonp跨域请求的问题

    今天发现json取数据的时候有时会报如下的错误: No 'Access-Control-Allow-Origin' header is present on the requested resourc ...

  7. jquery extend中

    var $=123; <src="jquery.js"> //加载jquery.js的时候           里面有句 _$=window.$,保存123的 //no ...

  8. unset是不能清除保存在本地电脑上的cookie的,用于session就可以(弄了半天原来是这样)

    unset($_COOKIE["historyWord[$wordId]"]); 这样是不行的,unset只是将变量在脚本运行时注销,但是cookie是写在客户端的,下一次还是可以 ...

  9. lable标签透明

    方法1: pictureBox1.Controls.Add(lable1);    //或 this.label1.Parent=pictureBox1;   lable1.BackColor=Col ...

  10. rails 配置使用mysql

    1 在gemfile中要添加 gem 'mysql2' 2 在mysql数据库中创建三个数据库 dept_dev dept_test dept_pro 3 配置文件 default: &def ...