时时跟踪网络状态基于对AFNetWorking的一个二次封装

#importtypedef

NS_ENUM(NSInteger, NetWorkStateType) {

NetWorkStateUnKnow, // 未知网络

NetWorkStateNot, // 没有网络

NetWorkStateWWAN, // 移动网络

NetWorkStateWIFI // WIFI网络

};

// 返回给controller枚举block

typedef void(^NetWorkState)(NetWorkStateType type);

@interface NetWorkStateManager : NSObject

+ (NetWorkStateManager *)shareInstance;

// bloc属性

@property (nonatomic, copy)NetWorkState block;

// 给controller的接口

- (void)reachabilityNetWorkState:(NetWorkState)netWorkState;

@end

#import "NetWorkStateManager.h"

@implementation NetWorkStateManager

+ (NetWorkStateManager *)shareInstance

{

static NetWorkStateManager *manager = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

manager = [[NetWorkStateManager alloc] init];

});

return manager;

}

- (void)reachabilityNetWorkState:(NetWorkState)netWorkState

{

// block赋值

self.block = netWorkState;

AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];

[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

switch (status) {

case AFNetworkReachabilityStatusUnknown:

NSLog(@"未知网络");

self.block(NetWorkStateUnKnow);

break;

case AFNetworkReachabilityStatusNotReachable: NSLog(@"没有网络");

self.block(NetWorkStateNot);

break;

case AFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"2G,3G,4G移动网络");

self.block(NetWorkStateWWAN);

break;

case AFNetworkReachabilityStatusReachableViaWiFi:

NSLog(@"wifi网络");

self.block(NetWorkStateWIFI);

break;

default:

break;

}

}];

[manager startMonitoring];

}

@end

你可能感兴趣的:(时时跟踪网络状态基于对AFNetWorking的一个二次封装)