如果你想在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. svn服务器搭建与迁移

    2016-11-21更新: 今天被svn的钩子搞了半天,网上找解决方法都无效,下午被我试出来了,特此记录. 在svn的钩子中可以使用update来更新配置文件,比如ansible的,puppet的,具 ...

  2. sql server filter table name

    https://stackoverflow.com/questions/26577464/how-to-find-a-table-in-sql-server-if-only-the-partial-t ...

  3. poj 3468 A Simple Problem with Integers(线段树 插线问线)

    #include<iostream> #include<stdio.h> #include<string.h> #define NN 2500000 using n ...

  4. hadoop2.x安装配置

    1.首先准备hadoop2.2.0的安装包,从官网获取,略. 2.加压安装包,进行配置.假设hadoop安装到/usr/hadoop-2.2.0目录,则进行如下配置: (1)/etc/profile配 ...

  5. [Selenium] The commonly used validation method

    Assert.assertTrue(tmpEl.getAttribute("class").contains("selected"),"The fol ...

  6. redhat 关机注销命令详解

    一.注销,关机,重启 注销系统的logout命令 1,Logout 注销是登陆的相对操作,登陆系统后,若要离开系统,用户只要直接下达logout命令即可: [root@localhost root]# ...

  7. Vue中devtools安装使用

    vue.js的devtools安装 安装 1.github下载地址:https://github.com/vuejs/vue-devtools 2.下载好后进入vue-devtools-master工 ...

  8. writing-mode属性

    writing-mode属性 最初只是ie中的属性,只有ie支持,现在在css3中谷歌,火狐也开始支持. 所以使用的时候就需要记住两套不同的语法,ie的私有属性和css3的规范属性 如果只需要兼容到i ...

  9. web_html-day1

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...

  10. docker容器管理基础

    1.命令: docker info #查看服务器上docker详细信息 docker search #搜索镜像 docker image pull nginx:1.14-alpine #下载一个镜像 ...