A、说明
在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:
(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
  WIFI\3G网络:自动下载高清图片
  低速网络:只下载缩略图
  没有网络:只显示离线的缓存数据
 
苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
 
注意:此功能只能用真机测试,iOS模拟器,一直都有wifi信号,即使关掉或者使用飞行模式.
 
B、监测网络状态
Reachability的使用步骤
添加框架SystemConfiguration.framework
包含头文件
#import "Reachability.h"

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 检测wifi状态
Reachability *wifiStatus = [Reachability reachabilityForLocalWiFi]; // 检测手机网络状态
Reachability *netStatus = [Reachability reachabilityForInternetConnection]; // 判断网络状态
if ([wifiStatus currentReachabilityStatus] != NotReachable) {
NSLog(@"正在使用wifi上网");
} else if ([netStatus currentReachabilityStatus] != NotReachable) {
NSLog(@"正在使用手机网络上网");
} else {
NSLog(@"没有网络");
}
}
 
C.实时监测网络状态
使用通知监控
网络状态类要发送通知给控制器
销毁控制器的时候一定要删除通知、停止发送消息
 
 //
// ViewController.m
// ReachabilityDemo
//
// Created by hellovoidworld on 15/1/28.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h"
#import "Reachability.h" @interface ViewController () @property(nonatomic, strong) Reachability *networkStatus; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 实时监控手机联网状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectNetworkStatus) name:kReachabilityChangedNotification object:nil]; // 开启通知发送
self.networkStatus = [Reachability reachabilityForInternetConnection];
[self.networkStatus startNotifier];
} - (void)dealloc {
// 停止发送通知
[self.networkStatus stopNotifier]; // 切记要删除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
} // 用WIFI
// [wifi currentReachabilityStatus] != NotReachable
// [conn currentReachabilityStatus] != NotReachable // 没有用WIFI, 只用了手机网络
// [wifi currentReachabilityStatus] == NotReachable
// [conn currentReachabilityStatus] != NotReachable // 没有网络
// [wifi currentReachabilityStatus] == NotReachable
// [conn currentReachabilityStatus] == NotReachable - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self detectNetworkStatus];
} - (void) detectNetworkStatus {
// 检测wifi状态
Reachability *wifiStatus = [Reachability reachabilityForLocalWiFi]; // 检测手机网络状态
Reachability *netStatus = [Reachability reachabilityForInternetConnection]; // 判断网络状态
if ([wifiStatus currentReachabilityStatus] != NotReachable) {
NSLog(@"正在使用wifi上网");
} else if ([netStatus currentReachabilityStatus] != NotReachable) {
NSLog(@"正在使用手机网络上网");
} else {
NSLog(@"没有网络");
}
} @end
 

[iOS 多线程 & 网络 - 2.8] - 检测网络状态的更多相关文章

  1. iOS 网络与多线程--1.检测网络链接状态

    通过Reachability库,检测设备的网络连接状况. 使用到的类库:Reachability Reachability库,是一个iOS环境下,检测设备网络状态的库,可以在网络上搜索下载. 使用之前 ...

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

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

  3. 李洪强iOS开发之使用 Reachability 检测网络

    1.iOS平台是按照一直有网络连接的思路来设计的,开发者利用这一特点创造了很多优秀的第三方应用. 大多数的iOS应用都需要联网,甚至有些应用严重依赖网络,没有网络就无法正常工作. 2.在你的应用尝试通 ...

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

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

  5. 「面向打野编程」iOS多线程:CGD

    「面向打野编程」iOS多线程:CGD 前言 参考网络其他文章而写,渣水平,抛砖引玉. 虽然Concurrent意思为并发,但由于队列的实际效果,以下称为并行队列. 当前iPhone的CPU核心数远小于 ...

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

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

  7. iOS检测网络连接状态

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

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

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

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

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

随机推荐

  1. SqlServer中获取数据库中每个表的行数

    CREATE TABLE #RowCounts(NumberOfRows BIGINT,TableName VARCHAR(128)) EXEC sp_MSForEachTable 'INSERT I ...

  2. 通过org.springframework.web.filter.CharacterEncodingFilter定义Spring web请求的编码

    通过类org.springframework.web.filter.CharacterEncodingFilter,定义request和response的编码.具体做法是,在web.xml中定义一个F ...

  3. build path功能详解

    在项目上右键>Build path>Config build path “web project”中,一般把"src"设置为source folder,把WEB-INF ...

  4. BZOJ_1029_[JSOI2007]_建筑抢修_(贪心+优先队列)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1029 \(n\)个任务需要完成,给出每个任务所需时间\(t_1\)与deadline\(t_2 ...

  5. ASP.NET MVC Html.ActionLink使用说明

    本文整理了该方法的几种重载形式: 1.Html.ActionLink("linkText","actionName")该重载的第一个参数是该链接要显示的文字,第 ...

  6. 注解框架ButterKnife

    将插件升级到1.3后支持Android Studio1.3 + ButterKnife7 如何使用 有所使用的布局 ID 上点击右键 (例如上图中的 R.layout.activity_setting ...

  7. uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)

    题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...

  8. git - svn 平滑到 git

    1. 建立自己的git仓库,需要是空git仓库 2. checkout 你的 git仓库 3. svn忽略.git文件,忽略.git  .gitignore 4. 把 .git文件拷到你的 svn仓库 ...

  9. erl0004 - ets 安全遍历

    safe_fixtable(Tab, true|false) -> true        Types:              Tab = tid() | atom() 锁定set,bag和 ...

  10. 【JS】<select>标签小结

    循环时通过<c:if>来判断是否为默认选中 <select name="select" id="month"> <c:forEac ...