首页 技术 正文
技术 2022年11月17日
0 收藏 777 点赞 3,519 浏览 3040 个字

本文转载至 http://blog.csdn.net/wangzi11322/article/details/45580917

检测网络状态

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

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

Reachability的使用步骤 
添加框架SystemConfiguration.framework 
iOS开发 – 检测网络状态(WIFI、2G/3G/4G) 
添加源代码 
iOS开发 – 检测网络状态(WIFI、2G/3G/4G) 
包含头文件

#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];
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,508
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,351
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,846