iOS开发 - 检测网络状态(WIFI、2G/3G/4G)

时间:2021-07-24 01:56:55

本文转载至 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];
}
}