项目中常常需要监听程序的网络变化,判断用户是以哪种上网方式,在网上搜索了一番,找到了两个常用的监听网络的方法,第一种是使用AFNetworking中的AFNetworkReachabilityManager,第二种是使用苹果推荐的Reachability,如果你以为我这里在写下它俩的用法,那你就错了(好吧,是我错了,还是要写下这两个的用法)。请看标题,为什么叫真·实时监听网络变化,这两种方法确实能监听到网络的状态,但是有一种情况,就是链接上了WiFi,但是这个WiFi是没有网的,此时用那两种方法只能判断当前设备链接的WiFi,无法判断能不能连接到互联网。努力上午搜寻了一下,发现一个开源的三方挺好用,这里分享下(先贴下AFNetworkReachabilityManager和Reachability,有些情况下还是需要它俩的,做个记录)。
以下都是在AppDelegate文件中编写的,有添加通知的是实时监听网络,未添加通知的地方如需要实时监听请自行添加,添加完别忘移除通知(有点啰嗦,谁让我记性差)。
AFNetworkReachabilityManager
-下载AFNetworking
- 包含头文件#import "AFNetworking.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self listenNetWorkingStatus]; //监听网络是否可用
}
-(void)listenNetWorkingStatus{
//1:创建网络监听者
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager manager];
//2:获取网络状态
/*
AFNetworkReachabilityStatusUnknown = 未知网络,
AFNetworkReachabilityStatusNotReachable = 没有联网
AFNetworkReachabilityStatusReachableViaWWAN = 蜂窝数据
AFNetworkReachabilityStatusReachableViaWiFi = 无线网
*/
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知网络");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"没有联网");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"蜂窝数据");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"无线网");
break;
default:
break;
}
}];
//开启网络监听
[manager startMonitoring];
}
Reachability
- 下载Reachability,把Reachability.h Reachability.m文件导入工程
- 包含头文件#import "Reachability.h"
- 生成两个全局对象
@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self listenNetWorkingStatus]; //监听网络是否可用
}
-(void)listenNetWorkingStatus{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
// 设置网络检测的站点
NSString *remoteHostName = @"www.apple.com";
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
[self.hostReachability startNotifier];
[self updateInterfaceWithReachability:self.hostReachability];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
}
- (void) reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
[self updateInterfaceWithReachability:curReach];
}
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
NetworkStatus netStatus = [reachability currentReachabilityStatus];
switch (netStatus) {
case 0:
NSLog(@"NotReachable----无网络");
break;
case 1:
NSLog(@"ReachableViaWiFi----WIFI");
break;
case 2:
NSLog(@"ReachableViaWWAN----蜂窝网络");
break;
default:
break;
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
真·实时监听网络变化 RealReachability
- 点上面的名称下载
- 把RealReachability文件夹拷贝到项目中
- 包含头文件#import "RealReachability.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self listenNetWorkingStatus]; //监听网络是否可用
}
-(void)listenNetWorkingStatus{
[GLobalRealReachability startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkChanged:)
name:kRealReachabilityChangedNotification
object:nil];
ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
[self realNetworkingStatus:status];
}
- (void)networkChanged:(NSNotification *)notification
{
RealReachability *reachability = (RealReachability *)notification.object;
ReachabilityStatus status = [reachability currentReachabilityStatus];
[self realNetworkingStatus:status];
}
-(void)realNetworkingStatus:(ReachabilityStatus)status{
switch (status)
{
case RealStatusUnknown:
{
NSLog(@"~~~~~~~~~~~~~RealStatusUnknown");
[self showNetworkStatusAlert:@"当前网络不可用"];
break;
}
case RealStatusNotReachable:
{
NSLog(@"~~~~~~~~~~~~~RealStatusNotReachable");
[self showNetworkStatusAlert:@"无网络,请检查网络链接"];
break;
}
case RealStatusViaWWAN:
{
NSLog(@"~~~~~~~~~~~~~RealStatusViaWWAN");
[self showNetworkStatusAlert:@"流量上网"];
break;
}
case RealStatusViaWiFi:
{
NSLog(@"~~~~~~~~~~~~~RealStatusViaWiFi");
// [self showNetworkStatusAlert:@"WIFI上网,尽情挥霍吧小宝贝~"];
break;
}
default:
break;
}
}
-(void)showNetworkStatusAlert:(NSString *)str{
//我这里是网络变化弹出一个警报框,由于不知道怎么让widow加载UIAlertController,所以这里用UIAlertView替代了
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
帮助到你的话请点喜欢并来波关注,点关注,不迷路(个人原创)