iOS检测网络连接状态
官方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检测网络连接状态的更多相关文章
- iOS开发 - Swift实现检测网络连接状态及网络类型
一.前言 在移动开发中,检测网络的连接状态尤其检测网络的类型尤为重要.本文将介绍在iOS开发中,如何使用Swift检测网络连接状态及网络类型(移动网络.Wifi). 二.如何实现 Reachabili ...
- [Swift通天遁地]四、网络和线程-(6)检测网络连接状态
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Delphi检测网络连接状态
有时候,我们做一些小软件就需要检测网络连接状态,比如想给你的软件加上类似QQ那样的系统消息,可是像我这样的穷人肯定是买不起服务器了,那我们只好另想办法,可以读取网页然后用浏览器显示,这个时候就需要判断 ...
- Android 检测网络连接状态
Android连接网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置. 首先,要判断网络状态,需要有相应的权限,下面为权限代码(Androi ...
- android检测网络连接状态示例讲解
网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置 Android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(Andro ...
- iOS 判断网络连接状态的几种方法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #801b80 } p.p2 ...
- qt检测网络连接状态【只能检测和路由器的连接,不能测试到外网的连接】
#include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityManager 类,用 ...
- Android编程 获取网络连接状态 及调用网络配置界面
获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...
随机推荐
- Filezilla Server 配置大全
一个开源.免费的FTP服务端程序,Windows 安装程序(0.9.50 beta):右键另存为. 首先说一下FTP的二种模式:主动模式(port mode)与被动模式(passive mode),网 ...
- C++开发者都应该使用的10个C++11特性
转载自http://blog.jobbole.com/44015/ 在C++11新标准中,语言本身和标准库都增加了很多新内容,本文只涉及了一些皮毛.不过我相信这些新特性当中有一些,应该成为所有C++开 ...
- J2EE学习中一些值得研究的开源项(转)
这篇文章写在我研究J2SE.J2EE近三年后.前3年我研究了J2SE的Swing.Applet.Net.RMI.Collections. IO.JNI……研究了J2EE的JDBC.Sevlet.JSP ...
- 1.scala语法
对象的apply方法 (1)对象调用apply()方法,可省略成() (2)string对象的apply方法返回第n个字符 "hello"(4) //'o' if语句的返回值 ja ...
- C4.5决策树算法概念学习
数据挖掘一般是指从大量的数据中自动搜索隐藏于其中的有着特殊关系性的信息的过程. •分类和聚类 •分类(Classification)就是按照某种标准给对象贴标签,再根据标签来区分归类,类别数不变. • ...
- web提前做好测试
1.压力测试,找到极限点和瓶颈,最小化扩容2.消息队列应对高并发的写操作 根据数据大小分成不同队列,保证效率 堵塞队列,压队列机极限处理能力3.主要业务和次要业务分开,当出现异常时保障主要业务,保证系 ...
- [实变函数]2.2 聚点 (cluster point), 内点 (interior point), 界点 (boundary point)
设 $E\subset \bbR^n, P_0\in \bbR^n$. 1 若 $\exists\ U(P_0)\subset E$, 则称 $P_0$ 为 $E$ 的内点 (interior poi ...
- 斑马打印机网卡ZebraNet配置(有线)
实图: 抽象图: 说明: 1.并口,用于连接斑马打印机一端 2.网络连接状态指示灯 3.打印状态指示灯 4.测试按钮,在连接打印机的情况下,按下此键,会打印出网卡信息. 5.网线接口 按下测试按钮之后 ...
- purple-class2-默认选项切换
ylbtech-class:purple-class2 A, 返回顶部 1,默认选项切换 #region 默认选项切换 public delegate IList<SelectListItemI ...
- 在java中使用正则表达式注意的地方
1. 对^与$的理解 通常我们会通过类似Matcher matcher = Pattern.compile(regex).matcher(string);的代码去拿到一个Matcher对象.这种情况下 ...