监控网络状态的改变

为了程序的安全性,在进行网络请求时,有必要判断网络连通性,有时也需要根据当前没有网/是wwan或wifi做不同的操作。

首先添加框架SystemConfiguration.framework,然后导入Reachability.h和Reachability.m。

(下载地址:http://download.csdn.net/download/yiran_style/4979061)

之后,可以对这两个库进行封装,下面记下我的个人封装:

ReachabilityChecker.h:

 1 #import <Foundation/Foundation.h>
 2 #import "Reachability.h"
 3 
 4 typedef void (^ActionBlock)();
 5 
 6 @class Reachability;
 7 
 8 @interface BBBReachabilityChecker : NSObject
 9 
10 @property (retain, nonatomic) Reachability *hostReach;
11 
12 //回调事件
13 @property (copy, nonatomic, readonly) ActionBlock notReachableEventBlock;
14 @property (copy, nonatomic, readonly) ActionBlock reachableViaWiFiEventBlock;
15 @property (copy, nonatomic, readonly) ActionBlock reachableViaWWANEventBlock;
16 
17 //当前网络状态
18 @property (assign, nonatomic, readonly) NetworkStatus currentNetworkStatus;
19 
20 //单例
21 + (BBBReachabilityChecker *)sharedInstance;
22 
23 //开启网络状态监听循环
24 - (void)beginListenerRunLoopAtAppDelegate:(id<UIApplicationDelegate>) appDelegate;
25 
26 //注册断网、切换到WWAN或WiFi时的事件
27 - (void)registOnStatusChangeToNotReachableEvent:(ActionBlock)block;
28 - (void)registOnStatusChangeToReachableViaWiFiEvent:(ActionBlock)block;
29 - (void)registOnStatusChangeToReachableViaWWANEvent:(ActionBlock)block;
30 
31 @end

ReachabilityChecker.m:

  1 #import "ReachabilityChecker.h"
  2 #import "Reachability.h"
  3 
  4 static ReachabilityChecker *_sharedInstance = nil;
  5 
  6 @implementation ReachabilityChecker
  7 
  8 #pragma mark - 单例构造
  9 + (BBBReachabilityChecker *)sharedInstance {
 10     if (_sharedInstance == nil) {
 11         @synchronized(self) {
 12             _sharedInstance = [[super allocWithZone:nil] init];
 13         }
 14     }
 15     return _sharedInstance;
 16 }
 17 
 18 +(id)allocWithZone:(struct _NSZone *)zone {
 19     return [[self sharedInstance] retain];
 20 }
 21 
 22 -(id)copyWithZone:(NSZone *)zone {
 23     return self;
 24 }
 25 
 26 -(id)init {
 27     if (_sharedInstance) {
 28         return _sharedInstance;
 29     }
 30     self = [super init];
 31     return self;
 32 }
 33 
 34 - (id)retain {
 35     return self;
 36 }
 37 
 38 - (oneway void)release {
 39     // Do nothing
 40 }
 41 
 42 -(id)autorelease {
 43     return self;
 44 }
 45 
 46 -(NSUInteger)retainCount {
 47     return NSUIntegerMax;
 48 }
 49 
 50 #pragma mark - 公有方法实现
 51 
 52 //开启网络状态监听循环
 53 - (void)beginListenerRunLoopAtAppDelegate:(__weak id<UIApplicationDelegate>) appDelegate {
 54     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pReachabilityChanged:) name: kReachabilityChangedNotification object: nil];
 55     self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
 56     [self.hostReach startNotifier];
 57 }
 58 
 59 #pragma mark - 私有方法实现
 60 
 61 //响应通知中心
 62 - (void)pReachabilityChanged:(NSNotification *)note
 63 {
 64     Reachability *curReach = [note object];
 65     [self pUpdateInterfaceWithReachability: curReach];
 66 }
 67 
 68 //处理连接改变后的情况
 69 - (void)pUpdateInterfaceWithReachability: (Reachability*) curReach
 70 {
 71     NetworkStatus status = [curReach currentReachabilityStatus];
 72     
 73     //记录网络状态
 74     _currentNetworkStatus = status;
 75     
 76     if (status == NotReachable) {
 77         if (_notReachableEventBlock != NULL) {
 78             _notReachableEventBlock();
 79         }
 80     }
 81     else if (status == ReachableViaWiFi) {
 82         if (_reachableViaWiFiEventBlock != NULL) {
 83             _reachableViaWiFiEventBlock();
 84         }
 85     }
 86     else if (status == ReachableViaWWAN) {
 87         if (_reachableViaWWANEventBlock != NULL) {
 88             _reachableViaWWANEventBlock();
 89         }
 90     }
 91 }
 92 
 93 #pragma mark - 注册断网、切换到WiFi/WWAN时的事件
 94 - (void)registOnStatusChangeToNotReachableEvent:(ActionBlock)block {
 95     if (_notReachableEventBlock != NULL) {
 96         Block_release(_notReachableEventBlock);
 97     }
 98     _notReachableEventBlock = [block copy];
 99 }
100 
101 - (void)registOnStatusChangeToReachableViaWiFiEvent:(ActionBlock)block {
102     if (_reachableViaWiFiEventBlock != NULL) {
103         Block_release(_reachableViaWiFiEventBlock);
104     }
105     _reachableViaWiFiEventBlock = [block copy];
106 }
107 
108 - (void)registOnStatusChangeToReachableViaWWANEvent:(ActionBlock)block {
109     if (_reachableViaWWANEventBlock != NULL) {
110         Block_release(_reachableViaWWANEventBlock);
111     }
112     _reachableViaWWANEventBlock = [block copy];
113 }
114 
115 @end

接下来只要在应用程序代理当中初始化这个类即可:

1     //开启网络状态监听
2     [[BBBReachabilityChecker sharedInstance] beginListenerRunLoopAtAppDelegate:self];

然后可以设置三个block来处理网络状态发生变化时的事件(当然,应该也可以利用target-action的形式进行封装),此外,这个封装还可以直接取到当前网络状态的枚举值。

原文地址:https://www.cnblogs.com/Steak/p/3802285.html