网络监控
2017-01-12 本文已影响4人
阿凡提说AI
1.在网络应用中,需要对用户设备的网络状态进行实时监控,目的是让用户了解自己的网络状态,防止一些误会(比如怪应用无能)
2.根据用户的网络状态进行智能处理,节省用户流量,提高用户体验
WIFI\3G网络:自动下载高清图片
低速网络:只下载缩略图
没有网络:只显示离线的缓存数据
3.苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
使用步骤:
(1)添加框架SystemConfiguration.framework
(2)添加下载好的源代码
(3)包含头文件
#import"Reachability.h"
(4) 常见用法
//是否WIFI
+(BOOL)IsEnableWIFI
{
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
//是否3G
+(BOOL)IsEnable3G {
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
4.网络监控
[[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];
}