检测网络状态

在网络应用中,需要对用户设备的网络状态进行实时监控,目的是
让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
WIFI\3G网络:自动下载高清图片
低速网络:只下载缩略图
没有网络:只显示离线的缓存数据

苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

Reachability的使用步骤
添加框架SystemConfiguration.framework

添加源代码

包含头文件

1
<code class="hljs ruleslanguage">#import "Reachability.h"</code>

抽取工具类

1
2
3
4
5
6
7
8
9
10
11
<code class="hljs objectivec">常见用法
// 是否WIFI
+ (BOOL) IsEnableWIFI {
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
 
// 是否3G
+ (BOOL) IsEnable3G {
    return ([[Reachability reachabilityForInternetConnectionen] currentReachabilityStatus] != NotReachable);
}
</code>

网络监控

1
2
3
4
5
6
7
8
9
10
11
<code class="hljs objectivec">[[NSNotificationCenter defaultCenter] addObserver:self
 selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
self.netReachability = [Reachability reachabilityForInternetConnection];
[self.netReachability startNotifier];
 
- (void)dealloc
{
    [self.netReachability stopNotifier];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
</code>

检测网络状态实例

NetWorkTool工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<code class="hljs java">#import <foundation foundation.h="">
 
@interface NetworkTool : NSObject
/**
 *  是否WIFI
 */
+ (BOOL)isEnableWIFI;
 
/**
 *  是否3G
 */
+ (BOOL)isEnable3G;
@end
</foundation></code>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<code class="hljs objectivec">#import "NetWorkTool.h"
#import "Reachability.h"
 
@implementation NetworkTool
// 是否WIFI
+ (BOOL)isEnableWIFI {
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
 
// 是否3G
+ (BOOL)isEnable3G {
    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
@end</code>

实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<code class="hljs objectivec">#import "ViewController.h"
#import "Reachability.h"
#import "NetworkTool.h"
 
@interface ViewController ()
@property (nonatomic, strong) Reachability *reachability;
@end
 
@implementation ViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    // 监听网络状态发生改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
 
    // 获得Reachability对象
    self.reachability = [Reachability reachabilityForInternetConnection];
    // 开始监控网络
    [self.reachability startNotifier];
 
}
 
- (void)dealloc
{
    [self.reachability stopNotifier];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
- (void)networkStateChange
{
    NSLog(@"网络状态改变了");
    [self checkNetworkState];
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self checkNetworkState];
}</code>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<code class="hljs objectivec">#pragma mark - App应用程序网络类型改变就会调用
- (void)networkStateChange
{
    if ([NetWorkTool isEnableWIFI]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示您" message:@"您现在网络环境为wifi!开始畅享吧!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else if ([NetWorkTool isEnable3G]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示您"  message:@"您现在网络环境为3G/4G网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示您" message:@"您当前没有可连的网络或已经掉线!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }

iOS 检测网络状态 自动判断 认为提示网络改变的更多相关文章

  1. iOS开发-关于网络状态的判断

    在判断网络状态这个问题上,苹果提供了一个叫Reachability的第三方库,但是这个库并不能真正的检测我们的网络状态,我也是在调试程序的时候发现的.详情可以阅读这个博客http://blog.csd ...

  2. AFN网络状态的时时监控以及网络的判断、

    //3.判断网络状况    AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager ...

  3. android 中获取网络状态、判断3G、2G、wifi网络、判断wifi是否打开、获取本机地址、获取本机串号IMEI整理

    代码如下:package com.android.xym; import java.io.IOException; import java.net.HttpURLConnection; import ...

  4. Android检测网络状态,判断当前网络是否可用

    用户手机当前网络可用:WIFI.2G/3G网络,用户打开与不打开网络,和是否可以用是两码事.可以使用指的是:用户打开网络了并且可以连上互联网进行上网. 检测当前网络是否可用,代码如下: /** * 检 ...

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

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

  6. iOS模拟各种网络状态

    在iOS开发中我们有在各种不同网络状态下测试app运行状态的需求.苹果给我们提供了在模拟器和真机状态下,模拟各种网络状态的软件. 在模拟器中 苹果提供的模拟网络状态的工具官网地址下载该工具需要登录Ap ...

  7. iOS网络-06-监听Iphone的网络状态

    使用系统的方法来监听网络状态 系统的方法是通过通知机制来实现网络状态的监听 实现网络状态监听的步骤 定义Reachability类型的成员变量来保存网络的状态 @property (nonatomic ...

  8. iOS开发之runtime的运用-获取当前网络状态

    之前写过runtime的一些东西,这次通过runtime获取一些苹果官方不想让你拿到的东西,比如,状态栏内部的控件属性.本文将通过runtime带你一步步拿到状态栏中显示网络状态的控件,然后通过监测该 ...

  9. Unity如何判断网络状态?

    根据Application.internetReachability来判断网络状态 NetworkReachability.NotReachable 网络不可用 NetworkReachability ...

随机推荐

  1. 第二篇:呈现内容_第二节:WebControl呈现

    一.WebControl的呈现过程 WebControl派生自Control类,所以WebControl的呈现功能基于Control的呈现逻辑之上,但有了比较大的扩展. 首先,WebControl重写 ...

  2. Knockout: 实践CSS绑定和afterkeydown事件, 给未通过校验的输入框添加红色边框突出显示; 使用afterkeydown事件自动将输入转大写字母.

    目的: 实践一下Knockout提供的CSS绑定功能和afterkeydown事件. 步骤: 先在htm中添加.error的css样式, 并在输入框中绑定css: { error: !firstNam ...

  3. django session入门详解

    概括性的讲: 1.django默认是打开对session的支持的 2.默认情况下session相关的数据会保存在数据库中.浏览器端只保存了session id session 的科普: 1.动态网站中 ...

  4. Eclipse maven问题汇总

    在使用eclipse+maven的过程中,遇到一系列问题,先汇总记录如下: 1. 在java工程中,缺少Maven依赖: 这个问题比较棘手,一般都对eclispe的工程结构不是很了解,后来经过长时间的 ...

  5. Android 性能优化总结

    App 流畅运行,是用户体验的第一步.App 流程运行,涉及到性能优化,主要涉及到布局优化, 绘制优化,内存泄漏优化,响应速度优化,列表展示优化,Bitmap 优化,线程优化,包大小优化. 布局优化 ...

  6. 腾讯云服务器 离线安装最新稳定版MariaDB 10.2.6

    数据库方面我们一般都是使用mysql,由于前段时间我们切换到了MariaDB后,当然生产环境也要更着变,谁叫oracle是个碧池呢! mariaDB主要有三种安装方式 源码安装,有点繁琐,不推荐 yu ...

  7. Atitit vue.js 把ajax数据 绑定到form表单

    Atitit vue.js 把ajax数据 绑定到form表单 1.1. 使用场景:主要应用在编辑与提交场合..1 1.2. 绑定数据到form控件,可以使用jquery,不过vue.js更加简单1 ...

  8. 深入理解Linux内核-I/O体系结构和设备驱动程序

    系统总线:1.链接CPU.RAM.I/O设备之间的数据流动.例如:PCI.ISA.EISA.MCA.SCSI.USB2.任何I\O设备有且仅能链接一条总线. I\O端口:1.每个连接到I\O总线上的设 ...

  9. vue全面介绍

    Vue.js 是一个JavaScriptMVVM库,是一套构建用户界面的渐进式框架.它是以数据驱动和组件化的思想构建的,采用自底向上增量开发的设计.相比于Angular.js,Vue.js提供了更加简 ...

  10. hdu 1217 Arbitrage (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1217 /************************************************* ...