iOS开发 - 检测网络状态(WIFI、2G/3G/4G)
本文转载至 http://blog.csdn.net/wangzi11322/article/details/45580917
检测网络状态
在网络应用中,需要对用户设备的网络状态进行实时监控,目的是
让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
WIFI\3G网络:自动下载高清图片
低速网络:只下载缩略图
没有网络:只显示离线的缓存数据
苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
Reachability的使用步骤
添加框架SystemConfiguration.framework
添加源代码
包含头文件
#import "Reachability.h"
- 1
抽取工具类
常见用法
// 是否WIFI
+ (BOOL) IsEnableWIFI {
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 是否3G
+ (BOOL) IsEnable3G {
return ([[Reachability reachabilityForInternetConnectionen] currentReachabilityStatus] != NotReachable);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
网络监控
[[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];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
检测网络状态实例
NetWorkTool工具类
#import <Foundation/Foundation.h>
@interface NetworkTool : NSObject
/**
* 是否WIFI
*/
+ (BOOL)isEnableWIFI;
/**
* 是否3G
*/
+ (BOOL)isEnable3G;
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
#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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
实现类
#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];
}
- 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
#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开发 - 检测网络状态(WIFI、2G/3G/4G)的更多相关文章
- iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G) by徐文棋
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- 利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- iOS 中如何判断当前是2G/3G/4G/5G/WiFi
5G 什么的,还得等苹果API更新啊,不过将来还是这个处理过程就是了. 关于判断当前的网络环境是2G/3G/4G,这个问题以前经常看到,最近在一工程里看到了如果判断的API.而在撸WebRTC音视频通 ...
- iOS开发-Reachability实时检测Wifi,2G/3G/4G/网络状态
最近遇到一个功能就是根据用户当前的网络状,用户未联网需要提示一下,如果是Wifi可以推荐一些图片新闻,如果是3G模式设置为无图的模式,获取网络状态比较简单,毕竟中国现在的流量还是一个比较贵的状态,哪天 ...
- iOS Reachability检测网络状态
一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发.其实在网络开发中还有比较常用的就是网络状 ...
- iOS开发系列-网络状态监控
概述 在网络应用中,需要对用户设别的网络状态进行实时监控,可以让用户了解自己的网络状态出现网络问题提示用户. 一般在网络状态不好的场景下需要做一些处理比如: WIFT/3G/4G网络:自动下载高清图. ...
- android 获取网络类型名称2G 3G 4G wifi
首先工程Manifest文件要引用: <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ...
- iOS开发-关于网络状态的判断
在判断网络状态这个问题上,苹果提供了一个叫Reachability的第三方库,但是这个库并不能真正的检测我们的网络状态,我也是在调试程序的时候发现的.详情可以阅读这个博客http://blog.csd ...
随机推荐
- QQ自动发送+@好友功能+tencent://功能
1.取出全部标题 D2007版本 procedure TForm1.Button1Click(Sender: TObject);var hCurrentWindow:HWnd; szText: a ...
- 后台js返回验证登陆绕过
思路: 程序员通过JS的返回数据来决定是否登陆成功.返回码为0000的时候表示登陆成功,将返回数据改为其登陆成功的即可. 具体过程: 后台登陆地址http://127.0.0.1/manager/ad ...
- SpringMVC 常用工具类与接口
ClassPathResource 在类路径下读取资源 public final String getPath()public boolean exists()public InputStream g ...
- linux防火墙开关以及端口规则添加
1. 重启后生效的 开启: chkconfig iptables on 关闭: chkconfig iptables off 2. 及时生效 开启: service iptables start 关闭 ...
- drupal 网址合集
Drupal7上传图片和文件重命名 http://drupalchina.cn/node/2168 Drupal 7 导入中文超时错误解决方法http://drupalchina.cn/node/17 ...
- 02 Architecture Overview
本章提要---------------------------------------------arthiecture, and some componentconnect to oracle这一章 ...
- kettle启动时候报a fatal exception has occurred
本人刚接触ETL工具 Data Integration - Kettle ,下载了kettle6.0版本,但是在window +jdk1.7(32位)下启动报错, 使用SpoonDebug.bat写的 ...
- R语言设置write.table()输出的文件格式
write.table(),是保存数据为文件的函数. > xiaohuqingdan <- c(3900088702, 3900072499,3900021029) > xiaohu ...
- Java 经典问题
九种基本类型及封装类 基本类型 boolean byte char short int long double void 二进制位数 1 8(一字节) 16(2字节) 16(2字节) 32(4字节) ...
- android hardware.c 源码分析
android的jni通过ID来找hal模块,使用了hw_get_module()函数,本文就通过这个函数的来分析一下hal层的模块是如何匹配的. 首先要了解三个结构体hw_module_t,hw_m ...