检测网络状态

在网络应用中,需要对用户设备的网络状态进行实时监控,目的是
让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
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. webpack window dev-server配置

    1.安装webpack dev-server npm install --save-dev webpack webpack-dev-server 著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  2. 【Visual Studio】Visual Studio中常用的快捷键收集

    添加注释:Ctr+k+c 取消注释:Ctr+k+u 格式化:Ctr+a+k+f 当前光标自动向下移一行:Ctr+Shift+Enter 运行:F5 进入光标指定的方法或是类:F12 在当前文件中查找: ...

  3. Linux命令-进程后台执行:nohup(就是不挂起的意思)

    nohup 就是不挂起的意思( no hang up) 用途:LINUX命令用法,不挂断地运行命令. 语法: nohup Command [ Arg ... ] [ & ] 描述:nohup ...

  4. Charles 网络抓包工具

    1.Charles 简介 Charles 是在 Mac.Linux 或 Windows 下常用的 http 协议网络包截取工具,在平常的测试与调式过程中,掌握此工具就基本可以不用其他抓包工具了.Cha ...

  5. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  6. 《TCP/IP详解卷1:协议》读书笔记

    <TCP/IP详解卷1:协议>读书笔记 - QingLiXueShi - 博客园https://www.cnblogs.com/mengwang024/p/4425834.html < ...

  7. java播放wav文件

    import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioFormat; import java ...

  8. Java 8 Streams filter examples

    1. Streams filter() and collect() package com.mkyong.java8; import java.util.Arrays;import java.util ...

  9. java中多个数字运算后值不对(失真)处理方法

    最近遇到一个bug ,在java里面计算两个数字相减,633011.20-31296.30 得到的结果居然是601714.8999999999,丢失精度了,原来这是Java浮点运算的一个bug. 解决 ...

  10. Windows 计划任务 Task Schedule 怎么 运行 .bat文件

    1. 新建 test.bat 需要切换文件夹以后 并且执行程序. cd /D "F:\xxxfolder" && dotnet testdll.dll 2. 直接设 ...