如果你想在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. 关于树论【动态树问题(LCT)】

    搬运:看一道caioj1439 题目描述 一开始给你一棵n个点n-1条边的树,每个点有一个权值wi. 三种操作: op=1 u v :在点u和点v之间建一条边. op=2 u v:摧毁点u到点v之间的 ...

  2. iOS--控制器加载自定义view的xib

    我们在项目中,经常需要使用到自定义的view,而xib布局显得更为简洁,那么如何加载一个自定义的xib呢,网上的方法也很多很多,就是因为太多了,我经常会弄混,所以总结其中一个使用,如果以后使用到其他的 ...

  3. HihoCoder1705: 座位问题(STL)

    描述 HIHO银行等待区有一排N个座位,从左到右依次编号1~N.现在有M位顾客坐在座位上,其中第i位坐在编号Ai的座位上. 之后又陆续来了K位顾客,(K + M ≤ N) 他们都会选择坐在最" ...

  4. BZOJ_4278_[ONTAK2015]Tasowanie_后缀数组

    BZOJ_4278_[ONTAK2015]Tasowanie_后缀数组 Description 给定两个数字串A和B,通过将A和B进行二路归并得到一个新的数字串T,请找到字典序最小的T. Input ...

  5. 利用ffmpeg0.6.1把.h264纯码流打包成.mp4 .avi等格式 (转载)

    转自:http://cache2.weidaohang.org/h/index.php?q=aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1cWluZ183MzkvYXJ0aWNsZS ...

  6. 51nod 1092【区间dp】

    思路: 简单的区间dp,从小区间到大区间,随便写. 还有一种是那啥,n-LCS...具体不说了,赶时间)))= =. #include <stdio.h> #include <str ...

  7. python __builtins__ staticmethod类 (64)

    64.'staticmethod', 返回静态方法 class staticmethod(object) | staticmethod(function) -> method | | Conve ...

  8. hdu 3038 How Many Answers Are Wrong【带权并查集】

    带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...

  9. TensorFlow多线程输入数据处理框架(四)——输入数据处理框架

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 输入数据处理的整个流程. #!/usr/bin/env python # -*- coding: UTF-8 -* ...

  10. Java关键字abstract与final总结

    关键字:abstract 用来修饰抽象类与抽象类中的方法 抽象类需要注意的几点: 抽象类不能被实例化.抽象类可以包含属性:方法:构造方法,但是构造方法不能用来new实例,只能被子类调用 有抽象方法的类 ...