一种网络监测工具的封装

版本记录

版本号 时间
V1.0 2017.04.08

前言

在代码中,我们经常需要进行网络请求数据。这里就有下面几种思路可供选择。

  1. 直接使用底层,判断网络状态,这种对编程人员要求比较高。对底层不了解的无法完成。
  2. 采用AFNetworking等第三方框架,使用类AFNetworkReachabilityManager进行判断。
  3. 在实际项目中很多情况是需要封装网络框架。这里面也有两种思路,一种将网络请求数据和网络监测封装在一起,而我们只需调用该单例,不用关心网络监测,因为内部已经实现了监测。另外一种就是网络数据请求和网络情况监测分别封装,在请求网络数据的时候先进行网络判断,同样也封装成一个单例。

这里我就简单的封装了个网络监测工具,希望大神多多指导。

详细设计

先看一下我的代码结构。

一种网络监测工具的封装_第1张图片
网络监测工具文档组织结构

然后直接看代码。

1. AppDelegate.m
#import "AppDelegate.h"
#import "JJTestVC.h"
#import "JJNetworkReachabilityTool.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[JJNetworkReachabilityTool shareNetworkReachabilityTool] application:application didFinishLaunchingWithOptions:launchOptions];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    JJTestVC *testVC = [[JJTestVC alloc] init];
    self.window.rootViewController = testVC;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [[JJNetworkReachabilityTool shareNetworkReachabilityTool] applicationWillTerminate:application];
}

@end

2. JJTestVC.m

#import "JJTestVC.h"
#import "JJNetworkReachabilityTool.h"

@interface JJTestVC ()

@end

@implementation JJTestVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    [button setTitle:@"网络状况" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.layer.borderWidth = 2;
    button.layer.borderColor = [UIColor blueColor].CGColor;
    [button addTarget:self action:@selector(buttonDidClick) forControlEvents:UIControlEventTouchUpInside];
    [button sizeToFit];
    button.center = self.view.center;
}

#pragma mark - 网络监测

- (void)buttonDidClick
{
    switch ([JJNetworkReachabilityTool shareNetworkReachabilityTool].currentStatus){
        case JJNetworkReachabilityToolNetStatusNotReachable:
            NSLog(@"没有网络");
            break;
            
        case JJNetworkReachabilityToolNetStatusReachableViaWWAN:
            NSLog(@"蜂窝网络");
            break;
            
        case JJNetworkReachabilityToolNetStatusReachableViaWiFi:
            NSLog(@"WiFi网络");
            
        default:
            break;
    }
}

@end

3. JJNetworkToolProtocol.h
#import 
#import 

@protocol JJNetworkToolProtocol 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

- (void)applicationWillResignActive:(UIApplication *)application;

- (void)applicationDidEnterBackground:(UIApplication *)application;

- (void)applicationWillEnterForeground:(UIApplication *)application;

- (void)applicationDidBecomeActive:(UIApplication *)application;

- (void)applicationWillTerminate:(UIApplication *)application;

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options;

@end

4. JJNetworkReachabilityTool.h
#import 
#import "JJNetworkToolProtocol.h"

typedef NS_ENUM(NSInteger, JJNetworkReachabilityToolNetStatus)
{
    JJNetworkReachabilityToolNetStatusUnknow = -1,
    JJNetworkReachabilityToolNetStatusNotReachable,
    JJNetworkReachabilityToolNetStatusReachableViaWWAN,
    JJNetworkReachabilityToolNetStatusReachableViaWiFi,
};

@interface JJNetworkReachabilityTool : NSObject 

@property (nonatomic,assign) JJNetworkReachabilityToolNetStatus currentStatus;
@property (nonatomic, assign, readonly, getter = isReachable) BOOL reachable;
@property (nonatomic, assign, readonly, getter = isReachableViaWWAN) BOOL reachableViaWWAN;
@property (nonatomic, assign, readonly, getter = isReachableViaWiFi) BOOL reachableViaWiFi;

+ (instancetype) shareNetworkReachabilityTool;

@end

5. JJNetworkReachabilityTool.m
#import "JJNetworkReachabilityTool.h"
#import "AFNetworking.h"

@interface JJNetworkReachabilityTool () 

- (void)startMonitoring;

- (void)stopMonitoring;

@end

@implementation JJNetworkReachabilityTool

#pragma mark - Override Base Function
- (instancetype)init
{
    if (self = [super init]) {
        self.currentStatus = JJNetworkReachabilityToolNetStatusUnknow;
    }
    return self;
}

#pragma mark - Class Public Function

+ (instancetype)shareNetworkReachabilityTool
{
    static JJNetworkReachabilityTool *reachablityTool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        reachablityTool = [[JJNetworkReachabilityTool alloc] init];
    });
    return reachablityTool;
}

#pragma mark - Object Private Function

- (void)startMonitoring
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

- (void)stopMonitoring
{
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    __weak typeof(self) weakSelf = self;
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWiFi:
                weakSelf.currentStatus = JJNetworkReachabilityToolNetStatusReachableViaWiFi;
                break;
               
            case AFNetworkReachabilityStatusReachableViaWWAN:
                weakSelf.currentStatus = JJNetworkReachabilityToolNetStatusReachableViaWWAN;
                break;
                
            default:
                weakSelf.currentStatus = JJNetworkReachabilityToolNetStatusNotReachable;
                break;
        }
    }];
    
    [self startMonitoring];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    [self stopMonitoring];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
    return YES;
}

#pragma mark - Getter Function

- (BOOL)isReachable
{
    return [self isReachableViaWiFi] || [self isReachableViaWWAN];
}

- (BOOL)isReachableViaWWAN
{
    return self.currentStatus == JJNetworkReachabilityToolNetStatusReachableViaWWAN;
}

- (BOOL)isReachableViaWiFi
{
    return self.currentStatus == JJNetworkReachabilityToolNetStatusReachableViaWiFi;
}

@end

测试结果

我们看下gif图。

一种网络监测工具的封装_第2张图片
网络检查

后记

  前面只是一个简单的工具,有不对的地方,希望大神留言和指正。也可以邮箱沟通,谢谢。

你可能感兴趣的:(一种网络监测工具的封装)