前言
APP项目中需要实时的检测网络状态是必须的,而且检测的框架很多如Reachability、AFNetworking以及RealReachability,本文所写的内容是采用RealReachability,今天内容的重点并不是实时监控网络状态变化,而是检测到网络变化实时刷新界面数据。
框架设计;
(1)不可或缺的网络状态监控以及网络状态保存,每次网络发生改变都发出通知;
(2)在基类XPQBaseViewController里面接收广播通知,通过传递过来的网络状态做相应的逻辑处理。
如何使用;
(1)封装好的网络监测单例类,每次网络发生改变,都会发出kLocalNetWorkChagnedNotofication通知;代码中的hasNet属性最好放在其他的单例类中,比如系统配置类AppContext等。
#import@interface NetWorkUtils : NSObject+ (instancetype)sharedNetWorkUtils;- (void)autoCheckNetWork;@end#import "NetWorkUtils.h"#import #import "GlobeConst.h"@interface NetWorkUtils ()@property (nonatomic, assign) BOOL hasNet;@end@implementation NetWorkUtils+ (instancetype)sharedNetWorkUtils{ static dispatch_once_t onceNetWorkToken; static NetWorkUtils *netWorkUtils; dispatch_once(& onceNetWorkToken, ^{ netWorkUtils = [[NetWorkUtils alloc] init]; }); return netWorkUtils;}- (void)autoCheckNetWork{ [GLobalRealReachability startNotifier]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kRealReachabilityChangedNotification object:nil];}- (void)networkChanged:(NSNotification *)notification{ RealReachability *reachability = (RealReachability *)notification.object; ReachabilityStatus status = [reachability currentReachabilityStatus]; switch (status) { case RealStatusNotReachable: { self.hasNet = NO; [[NSNotificationCenter defaultCenter] postNotificationName:NSLocalNetWorkChagnedNotoficationKey object:nil userInfo:@{ @"netType": @"RealStatusNotReachable" }]; break; } case RealStatusUnknown: { self.hasNet = YES; [[NSNotificationCenter defaultCenter] postNotificationName:NSLocalNetWorkChagnedNotoficationKey object:nil userInfo:@{ @"netType": @"RealStatusUnknown" }]; break; } case RealStatusViaWiFi: { self.hasNet = YES; [[NSNotificationCenter defaultCenter] postNotificationName:NSLocalNetWorkChagnedNotoficationKey object:nil userInfo:@{ @"netType": @"RealStatusViaWiFi" }]; break; } case RealStatusViaWWAN: { self.hasNet = YES; [[NSNotificationCenter defaultCenter] postNotificationName:NSLocalNetWorkChagnedNotoficationKey object:nil userInfo:@{ @"netType": @"RealStatusViaWWAN" }]; break; } default: break; }}@end
(2)公共基类XPQBaseViewController,接收kLocalNetWorkChagnedNotofication通知,并执行- (void)refreshLoadDataBase:(NSNotification *)notification方法;子类通过继承XPQBaseViewController结合业务需求在refreshLoadDataBase方法中,实现相应的业务逻辑。
#import@interface XPQBaseViewController : UIViewController- (void)refreshLoadDataBase:(NSNotification *)notification;@end#import "XPQBaseViewController.h"#import "GlobeConst.h"@interface XPQBaseViewController ()@end@implementation XPQBaseViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshLoadDataBase:) name:NSLocalNetWorkChagnedNotoficationKey object:nil];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)refreshLoadDataBase:(NSNotification *)notification { NSLog(@"开始重新加载网络数据");}- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:NSLocalNetWorkChagnedNotoficationKey object:nil];}@end
总结;
RealReachability检测网络状态框架除了自动检测,其实还可以手动检测,对于有在发起网络请求时需要实时检测网络连接情况的业务需求,RealReachability使用是比较方便的。