一般有两种方式,都是第三方的框架,轮子嘛,能用就先用着,后面再优化。

一: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 检测网络状态的更多相关文章

  1. iOS 检测网络状态 自动判断 认为提示网络改变

    检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是让用户了解自己的网络状态,防止一些误会(比如怪应用无能)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验WIFI\3G ...

  2. iOS开发——网络篇——数据安全(MD5),HTTPS,检测网络状态

    一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 ...

  3. iOS开发网络篇—Reachability检测网络状态

    前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...

  4. iOS检测网络连接状态

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

  5. [iOS 多线程 & 网络 - 2.8] - 检测网络状态

    A.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验 ...

  6. iOS开发 - 检测网络状态(WIFI、2G/3G/4G)

    本文转载至 http://blog.csdn.net/wangzi11322/article/details/45580917 检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是 ...

  7. linux c 检测网络状态

    转自:http://stackoverflow.com/questions/808560/how-to-detect-the-physical-connected-state-of-a-network ...

  8. iOS网络4——Reachability检测网络状态

    一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发. 其实在网络开发中还有比较常用的就是网络 ...

  9. iOS Reachability检测网络状态

    一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发.其实在网络开发中还有比较常用的就是网络状 ...

随机推荐

  1. about js

    function: javascript jquery modernizr yepnope code organization requirejs backbonejs http://blog.csd ...

  2. Ubuntu14.04强化之conky——Harmattan主题

    一共有17个主题,四种模式,两种天气模式,支持摄氏度和华氏度. This conky comes with 17 themes: Cards Elementary Elune Flatts Metro ...

  3. 在Python中调用C++,使用SWIG

    http://www.coder4.com/archives/2141 SWIG:Simplified Wrapper and Interface Generator,顾名思义,就是将C/C++包装为 ...

  4. easyui源码翻译1.32--Tree(树)

    前言 使用$.fn.tree.defaults重写默认值对象.下载该插件翻译源码 树控件在web页面中一个将分层数据以树形结构进行显示.它提供用户展开.折叠.拖拽.编辑和异步加载等功能. 源码 /** ...

  5. ANDROID_MARS学习笔记_S02_004_ExpandableListActivity

    1.main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  6. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

  7. hbase安装(zookeeper等)

    文库:http://wenku.baidu.com/link?url=5mnYL7ZuxUBWZnrnmak4JRVF5fJquJmjgmZy788i7UW8lUk4QXD8Nc_haPz33vjt9 ...

  8. [译]GotW #4 Class Mechanics

    你对写一个类的细节有多在行?这条款不仅注重公然的错误,更多的是一种专业的风格.了解这些原则将会帮助你设计易于使用和易于管理的类. JG Question 1. 什么使得接口“容易正确使用,错误使用却很 ...

  9. 在运行时切换 WinForm 程序的界面语言 ---------多语言设置基础

    System.ComponentModel.ComponentResourceManager .ApplyResources 时间:2015-06-17 14:59:06      阅读:473    ...

  10. poj3254Corn Fields(状压)

    http://poj.org/problem?id=3254 第一个状压题 思路挺好想 用二进制表示每行的状态 然后递推 用左移 右移来判断是不是01间隔出现 c大神教的 我的代码WA在这个地方了.. ...