iOS 检测网络状态 自动判断 认为提示网络改变
检测网络状态
在网络应用中,需要对用户设备的网络状态进行实时监控,目的是
让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
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 检测网络状态 自动判断 认为提示网络改变的更多相关文章
- iOS开发-关于网络状态的判断
在判断网络状态这个问题上,苹果提供了一个叫Reachability的第三方库,但是这个库并不能真正的检测我们的网络状态,我也是在调试程序的时候发现的.详情可以阅读这个博客http://blog.csd ...
- AFN网络状态的时时监控以及网络的判断、
//3.判断网络状况 AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager ...
- android 中获取网络状态、判断3G、2G、wifi网络、判断wifi是否打开、获取本机地址、获取本机串号IMEI整理
代码如下:package com.android.xym; import java.io.IOException; import java.net.HttpURLConnection; import ...
- Android检测网络状态,判断当前网络是否可用
用户手机当前网络可用:WIFI.2G/3G网络,用户打开与不打开网络,和是否可以用是两码事.可以使用指的是:用户打开网络了并且可以连上互联网进行上网. 检测当前网络是否可用,代码如下: /** * 检 ...
- iOS开发网络篇—Reachability检测网络状态
前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...
- iOS模拟各种网络状态
在iOS开发中我们有在各种不同网络状态下测试app运行状态的需求.苹果给我们提供了在模拟器和真机状态下,模拟各种网络状态的软件. 在模拟器中 苹果提供的模拟网络状态的工具官网地址下载该工具需要登录Ap ...
- iOS网络-06-监听Iphone的网络状态
使用系统的方法来监听网络状态 系统的方法是通过通知机制来实现网络状态的监听 实现网络状态监听的步骤 定义Reachability类型的成员变量来保存网络的状态 @property (nonatomic ...
- iOS开发之runtime的运用-获取当前网络状态
之前写过runtime的一些东西,这次通过runtime获取一些苹果官方不想让你拿到的东西,比如,状态栏内部的控件属性.本文将通过runtime带你一步步拿到状态栏中显示网络状态的控件,然后通过监测该 ...
- Unity如何判断网络状态?
根据Application.internetReachability来判断网络状态 NetworkReachability.NotReachable 网络不可用 NetworkReachability ...
随机推荐
- SpringBoot配置属性之Migration
SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...
- win7系统部分软件显示乱码怎么办
用了Win7以后,发现有的中文软件打开后,在界面上出现很多文字乱码,之前这个软件在XP上用过,一直都是中文的,怎么到Win7上,就显示乱码了. 到网上一查,发现很多网友都有同样问题,经过一番查找,找到 ...
- 基于key/value+Hadoop HDFS 设计的存储系统的shell命令接口
对于hadoop HDFS 中的全部命令进行解析(当中操作流程是自己的想法有不允许见欢迎大家指正) 接口名称 功能 操作流程 get 将文件拷贝到本地文件系统 . 假设指定了多个源文件,本地目的端必须 ...
- Python控制台输出带颜色的文字(高亮显示)方法
在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想要的信 ...
- gcc cc1: all warnings being treated as errors
cc1: all warnings being treated as errors 在Makefile中找到 -Werror项,删除即可.删除后重新编译. 或设置环境变量 c工程设置 export C ...
- 温故而知新 Ajax 的新坑 dataType: 'json'
为了方便实验,我随便捏造了一个json数据,然后放在php中输出. 请求明明是200,json数据也正确,但ajax就是不执行success回调? 原因是 dataType: 'json', 导致的. ...
- Android开发学习总结——搭建最新版本的Android开发环境
原文出自:https://www.cnblogs.com/xdp-gacl/p/4322165.html#undefined 最近由于工作中要负责开发一款Android的App,之前都是做JavaWe ...
- activiti设置流程变量
public static void mian(String args[]){ ProcessEngine processEngine = ProcessEngine.getDefaultProce ...
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
1. 要求 对列表(List)中的自定义对象,要求能够按照对象的属性(字段)进行排序(正序.倒序). 如:用户对象(Member)有用户名(username).级别(level).出生日期(birth ...
- 关于Verilog中的几种赋值语句
1. 连续赋值语句(Continuous Assignments) 连续赋值语句是Verilog数据流建模的基本语句,用于对线网进行赋值,等价于门级描述,是从更高的抽象角度来对电路进行描述.连续赋值语 ...