iOS 检测网络状态
一般有两种方式,都是第三方的框架,轮子嘛,能用就先用着,后面再优化。
一:Reachability
1.首先在AppDelegate.h添加头文件"Reachability.h",导入框架SystemConfiguration.frame。
2. 在AppDelegate.m中这样实现:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//开启网络状况的监听
//来订阅实时的网络状态变化通知。导入Reachability.h头文件,然后注册一个对象来订阅网络状态变化的信息,网络状态变化的信息名称为kReachabilityChanged-Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
//通过检查某个主机能否访问来判断当前网络是否可用:
self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
//开始监听,会启动一个run loop
[self.hostReach startNotifier];
}
-(void)reachabilityChanged:(NSNotification *)note{
Reachability *currReach = [note object];
NSParameterAssert([currReach isKindOfClass:[Reachability class]]);
//对连接改变做出响应处理动作
NetworkStatus status = [currReach currentReachabilityStatus];
//如果没有连接到网络就弹出提醒实况
self.isReachable = YES;
if(status == NotReachable){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
self.isReachable = NO;
return;
}
if (status==kReachableViaWiFi||status==kReachableViaWWAN) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
// [alert show];
[alert release];
self.isReachable = YES;
}
}
然后在每个页面的viewWillAppear:加上:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDlg.isReachable){
NSLog(@"网络已连接");//执行网络正常时的代码
}
else{
NSLog(@"网络连接异常");//执行网络异常时的代码
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
这样就可以检查到在运行程序时网络突然的中断和连接。Reachability类实际上是苹果公司对SCNetworkReachability API的封装,这个API定义在SystemConfigure.framework库中。如果有其他特别的需求,也可以直接使用这个原生的SCNetworkReachability类。
二:AFNetworking监测
1.导入框架,和头文件#import <AFNetworkReachabilityManager.h>
2.代码:
-(void)afn{
//1.创建网络状态监测管理者
AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
//开启监听,记得开启,不然不走block
[manger startMonitoring];
//2.监听改变
[manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/*
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
*/
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"没有网络");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G|4G");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi");
break;
default:
break;
}
}];
}
iOS 检测网络状态的更多相关文章
- iOS 检测网络状态 自动判断 认为提示网络改变
检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是让用户了解自己的网络状态,防止一些误会(比如怪应用无能)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验WIFI\3G ...
- iOS开发——网络篇——数据安全(MD5),HTTPS,检测网络状态
一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 ...
- iOS开发网络篇—Reachability检测网络状态
前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...
- iOS检测网络连接状态
官方Demo下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 将Reachab ...
- [iOS 多线程 & 网络 - 2.8] - 检测网络状态
A.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验 ...
- iOS开发 - 检测网络状态(WIFI、2G/3G/4G)
本文转载至 http://blog.csdn.net/wangzi11322/article/details/45580917 检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是 ...
- linux c 检测网络状态
转自:http://stackoverflow.com/questions/808560/how-to-detect-the-physical-connected-state-of-a-network ...
- iOS网络4——Reachability检测网络状态
一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发. 其实在网络开发中还有比较常用的就是网络 ...
- iOS Reachability检测网络状态
一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发.其实在网络开发中还有比较常用的就是网络状 ...
随机推荐
- MyBatis之传入参数——parameterType(转)
鸣谢:http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 ------------------------------------ ...
- Excel Cannot Connect to SharePoint List
As I am working in SharePoint support, I come across so many issues on day 2 day basis and always tr ...
- 自定义NavigationView's item 的高度
http://stackoverflow.com/questions/31204320/how-can-i-change-the-navigationviews-item-text-size 自定义s ...
- 近期刷题的c语言总结。
首先是三个数学函数... /* 函数名: floor 功 能: 下舍入,返回小于或者等于指定表达式的最大整数 说明:返回x的下限,如74.12的下限为74,-74.12的下限为-75.返回值为floa ...
- Server.MapPath 的使用方法
Server.MapPath 的使用方法 用法: 1.Server.MapPath ("/") 应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\ 2.Serve ...
- Item Import: What Does "Sync" Items Do? (Doc ID 417887.1)
In this Document Goal Solution APPLIES TO: Oracle Item Master - Version 11.5.10.0 to 11.5.10.0 [Rele ...
- 指令 scope
angular学习笔记(三十)-指令(8)-scope <!DOCTYPE html> <html ng-app="myApp"> <head> ...
- 利用URLRewriter重写url地址
首先,当然是下载URLRewriter了 download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDN ...
- ASP.NET MVC 3.0 Controller基础
ASP.NET MVC 3.0 Controller基础 1.Controller类与方法 Controller(控制器)是ASP.NET MVC的核心,负责处理浏览器请求,并作出响应.Cotro ...
- HashSet的实现原理
HashSet定义 public class HashSet<E> extends AbstractSet<E> implements Set<E>, Clonea ...