前言网络
APP项目中须要实时的检测网络状态是必须的,并且检测的框架不少如Reachability、AFNetworking以及RealReachability,本文所写的内容是采用RealReachability,今天内容的重点并非实时监控网络状态变化,而是检测到网络变化实时刷新界面数据。框架
框架设计;post
(1)不可或缺的网络状态监控以及网络状态保存,每次网络发生改变都发出通知;atom
(2)在基类XPQBaseViewController里面接收广播通知,经过传递过来的网络状态作相应的逻辑处理。spa
如何使用;设计
(1)封装好的网络监测单例类,每次网络发生改变,都会发出kLocalNetWorkChagnedNotofication通知;代码中的hasNet属性最好放在其余的单例类中,好比系统配置类AppContext等。code
#import <Foundation/Foundation.h> @interface NetWorkUtils : NSObject + (instancetype)sharedNetWorkUtils; - (void)autoCheckNetWork; @end #import "NetWorkUtils.h" #import <RealReachability.h> #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方法中,实现相应的业务逻辑。server
#import <UIKit/UIKit.h> @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使用是比较方便的。rem