前言網絡
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