如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote)。那么你会使用到Reachability来实现网络检测。

写本文的目的

  • 了解Reachability都能做什么
  • 检测3中网络环境
    • 2G/3G
    • wifi
    • 无网络
  • 如何使用通知
    • 单个controller
    • 多个controller
  • 简单的功能:
    • 仅在wifi下使用

Reachability简介

Reachablity 是一个iOS下检测,iOS设备网络环境用的库。

  • 监视目标网络是否可用
  • 监视当前网络的连接方式
  • 监测连接方式的变更

苹果官方提供的Doc

http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

Github上的文档

https://github.com/tonymillion/Reachability

安装

  1. 创建 network 工程(network是我创建的demo工程,附件中可以下载到)
  2. 使用Cocoaspod安装依赖
  3. 在项目中添加 SystemConfiguration.framework 库

由于Reachability非常常用。直接将其加入到Supporting Files/networ-Prefix.pch中:

  1. #import <Reachability/Reachability.h>

如果你还不知道cocoaspod是什么,看这里:

http://witcheryne.iteye.com/blog/1873221

使用

stackoverflow上有一篇回答,很好的解释了reachability的用法

http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability

  • 一般情况一个Reachability实例就ok了。
  • 一个Controller只需要一个Reachability

Block方式使用

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. DLog(@"开启 www.apple.com 的网络检测");
  5. Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
  6. DLog(@"-- current status: %@", reach.currentReachabilityString);
  7. // start the notifier which will cause the reachability object to retain itself!
  8. [[NSNotificationCenter defaultCenter] addObserver:self
  9. selector:@selector(reachabilityChanged:)
  10. name:kReachabilityChangedNotification
  11. object:nil];
  12. reach.reachableBlock = ^(Reachability * reachability)
  13. {
  14. dispatch_async(dispatch_get_main_queue(), ^{
  15. self.blockLabel.text = @"网络可用";
  16. self.blockLabel.backgroundColor = [UIColor greenColor];
  17. });
  18. };
  19. reach.unreachableBlock = ^(Reachability * reachability)
  20. {
  21. dispatch_async(dispatch_get_main_queue(), ^{
  22. self.blockLabel.text = @"网络不可用";
  23. self.blockLabel.backgroundColor = [UIColor redColor];
  24. });
  25. };
  26. [reach startNotifier];
  27. }

使用notification的方式

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. DLog(@"开启 www.apple.com 的网络检测");
  5. Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
  6. DLog(@"-- current status: %@", reach.currentReachabilityString);
  7. // start the notifier which will cause the reachability object to retain itself!
  8. [[NSNotificationCenter defaultCenter] addObserver:self
  9. selector:@selector(reachabilityChanged:)
  10. name:kReachabilityChangedNotification
  11. object:nil];
  12. [reach startNotifier];
  13. }
  14. - (void) reachabilityChanged: (NSNotification*)note {
  15. Reachability * reach = [note object];
  16. if(![reach isReachable])
  17. {
  18. self.notificationLabel.text = @"网络不可用";
  19. self.notificationLabel.backgroundColor = [UIColor redColor];
  20. self.wifiOnlyLabel.backgroundColor = [UIColor redColor];
  21. self.wwanOnlyLabel.backgroundColor = [UIColor redColor];
  22. return;
  23. }
  24. self.notificationLabel.text = @"网络可用";
  25. self.notificationLabel.backgroundColor = [UIColor greenColor];
  26. if (reach.isReachableViaWiFi) {
  27. self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];
  28. self.wifiOnlyLabel.text = @"当前通过wifi连接";
  29. } else {
  30. self.wifiOnlyLabel.backgroundColor = [UIColor redColor];
  31. self.wifiOnlyLabel.text = @"wifi未开启,不能用";
  32. }
  33. if (reach.isReachableViaWWAN) {
  34. self.wwanOnlyLabel.backgroundColor = [UIColor greenColor];
  35. self.wwanOnlyLabel.text = @"当前通过2g or 3g连接";
  36. } else {
  37. self.wwanOnlyLabel.backgroundColor = [UIColor redColor];
  38. self.wwanOnlyLabel.text = @"2g or 3g网络未使用";
  39. }
  40. }

附件demo说明

开启wifi状态

关闭wifi的状态

遗留问题

    1. 如何在多个controller之前共用一个Reachability(附件demo中是一个controller一个Reachability实例)
    2. 应该在什么使用停止Reachability的检测.

iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G的更多相关文章

  1. iOS中使用 Reachability 检测网络

    iOS中使用 Reachability 检测网络 内容提示:下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测.   写本文的目的 了解Reachability都 ...

  2. 【开发记录】iOS中使用 Reachability 检测网络

    如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Re ...

  3. SDWebImage -- 封装 (网络状态检测,是否打开手机网络下下载高清图设置)

    对SDWebImage 进行封装,为了更好的节省用户手机流量,并保证在移动网络下也展示高清图,对使用SDWebImage 下载图片之前进行逻辑处理,根据本地缓存中是否有缓存原始的图片,用户是否打开移动 ...

  4. iOS 中如何判断当前是2G/3G/4G/5G/WiFi

    5G 什么的,还得等苹果API更新啊,不过将来还是这个处理过程就是了. 关于判断当前的网络环境是2G/3G/4G,这个问题以前经常看到,最近在一工程里看到了如果判断的API.而在撸WebRTC音视频通 ...

  5. wp检测是否是手机网络还是wifi网络

    原文发布时间为:2013-06-22 -- 来源于本人的百度文章 [由搬家工具导入] ),newNameResolutionCallback(handle =>{NetworkInterface ...

  6. Android监听手机网络变化

    Android监听手机网络变化 手机网络状态发生变化会发送广播,利用广播接收者,监听手机网络变化 效果图 注册广播接收者 <?xml version="1.0" encodi ...

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

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

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

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

  9. iOS Reachability检测网络状态

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

随机推荐

  1. 西门子PLC存储器、地址区

    S7-1500 CPU的存储器 1.内部集成的存储器:工作存储器,保持性存储器,系统存储器 2.外插的SIMATIC存储卡:装载存储器去 装载存储器:断电信息不丢失,主要存储项目中程序块,数据块,工艺 ...

  2. Getting Started with the Intel Media SDK

    By Gael Hofemeier on March 19, 2015 Follow Gael on Twitter: @GaelHof Media SDK Developer’s Guide Med ...

  3. RPi 2B IPC webcam server

    /**************************************************************************** * RPi 2B IPC webcam se ...

  4. boost之timer

    1. timer类实现 #pragma once #include <ctime> #include <limits> class timer { public: timer( ...

  5. 当你触摸并按住触摸目标时候,禁止系统默认菜单-webkit-touch-call

    当你触摸并按住触摸目标时候,禁止或显示系统默认菜单. -webkit-touch-callout 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS ...

  6. bzoj4516

    后缀自动机 留个板子 upd:大概懂了 每次新加入的npRight集合肯定只有最后一个位置,那么求所有长得不一样的子串贡献就是Max-Min+1,因为Right集合只有这一个位置,所以这Max-Min ...

  7. C# 利用Aspose.Slides.dll将本地ppt文档转化成pdf(完美破解版 无水印 无中文乱码)

    下载Aspose.Slides.dll   http://pan.baidu.com/s/1kVPjnzL 添加引用C#代码. using System; using System.Collectio ...

  8. 洛谷 - P1111 - 修复公路 - 并查集

    https://www.luogu.org/problemnew/solution/P1111 并查集的水题,水题都错了好多发. 首先并不是有环就退出,而是连通分支为1才退出,每次合并成功连通分支才会 ...

  9. HDU6050: Funny Function(推公式+矩阵快速幂)

    传送门 题意 利用给出的式子求\(F_{m,1}\) 分析 直接推公式(都是找规律大佬) \(n为偶数,F_{m,1}=\frac{2(2^n-1)^{m-1}}3\) \(n为奇数,F_{m,1}= ...

  10. sql server编写通用脚本自动统计各表数据量心得

    工作过程中,如果一个数据库的表比较多,手工编写统计脚本就会比较繁琐,于是摸索出自动生成各表统计数据量脚本的通用方法,直接上代码: /* 脚本来源:https://www.cnblogs.com/zha ...