最近项目有视频播放,当然就需要监测用户网络,在变成运营商网络的时候给出提示,找到一个不错的东西。做一下记录,方便自己和有需要的人。
RealReachability 不错的对系统网络变更的检测
集成
最简便的集成方法当属pod: pod 'RealReachability'。
手动集成:将RealReachability文件夹加入到工程即可。
依赖:Xcode5.0+,支持ARC, iOS6+.项目需要引入SystemConfiguration.framework.
Installation with Carthage
Carthageis a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage withHomebrewusing the following command:
$ brew update
$ brew install carthage
RealReachability in yourCartfile:
github "dustturtle/RealReachability"
使用方法 :
在使用的地方或者PCH文件导入 头文件:#import "RealReachability.h"
typedefenum: NSInteger {
NotReachable = 0,//无连接
ReachableViaWiFi,//使用3G/GPRS网络
ReachableViaWWAN//使用WiFi网络
} NetworkStatus;
初始化:
在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions :加入:
[GLobalRealReachability startNotifier];
1.在需要动态监测的地方加入通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkChanged:)
name:kRealReachabilityChangedNotification
object:nil];
通知方法:
- (void)networkChanged:(NSNotification *)notification
{
RealReachability *reachability = (RealReachability *)notification.object;
ReachabilityStatus status = [reachability currentReachabilityStatus];
NSLog(@"currentStatus:%@",@(status));
}
2.在进入之前需要检测的地方检测网络环境(实时网络状态查询代码)
ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
block方式:[GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {
switch(status) {
caseRealStatusNotReachable:
{//case NotReachable handlerbreak; }
caseRealStatusViaWiFi: {//case WiFi handlerbreak; }
caseRealStatusViaWWAN: {//case WWAN handlerbreak; }
default:break;
} }];
WWANAccessType accessType = [GLobalRealReachability currentWWANtype];//运营商网络状态