单军华
2018-03-28 f99cf1d5cc50407394501853be06cb39f38a092c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
//  NetworkUnit.m
//  zxwTool
//
//  Created by zhuxuwei on 14-3-28.
//  Copyright (c) 2014年 zxwo0o@qq.com. All rights reserved.
//
 
#import "NetworkUnit.h"
 
@implementation NetworkUnit
 
 
+(id)sharedInstance
{
    static id sharedInstance;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[NetworkUnit alloc] init];
        
        //[sharedInstance addObserverReachabilityChanged];
    });
    
    return sharedInstance;
}
/**
 *  @author zhangchun, 15-11-27 23:11:43
 *
 *  delay time
 *
 *  @param netStatus
 *
 *  @return delayTime
 */
-(NSInteger)delayTime:(NetworkStatus) netStatus
{
    
    NSInteger delayTime =20;
    switch (netStatus) {
        case ReachableViaWiFi:
            delayTime = 10;
            break;
//        case ReachableVia2G:
//            delayTime = 20;
//
//            break;
//        case ReachableVia3G:
//            delayTime = 15;
//
//            break;
//        case ReachableVia4G:
//            delayTime = 10;
 
            break;
        default:
            break;
    }
    
    
    return delayTime;
}
 
+(NETWORK_TYPE)getNetworkTypeFromStatusBar {
    
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
    NSNumber *dataNetworkItemView = nil;
    
    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }
    NETWORK_TYPE nettype = NETWORK_TYPE_NONE;
    NSNumber * num = [dataNetworkItemView valueForKey:@"dataNetworkType"];
    int type = [num intValue];
    if (type == 5) {
        nettype = NETWORK_TYPE_WIFI;
    }else if (type == 1){
        nettype = NETWORK_TYPE_2G;
    }else if (type == 2){
        nettype = NETWORK_TYPE_3G;
    }else if (type == 3){
        nettype = NETWORK_TYPE_4G;
    }
    
    return nettype;
}
 
 
-(void)addObserverReachabilityChanged:(void(^)(NetworkStatus netStatus)) netStatusBlock{
 
    Reachability *_internetReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    [_internetReach startNotifier];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(reachabilityChangedNet:) name: kReachabilityChangedNotification object: nil];
    self.netStatusBlock = [netStatusBlock copy];
    
}
 
-(void)reachabilityChangedNet:(NSNotification* )note{
    
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    if (self.netStatusBlock) {
        self.netStatusBlock(netStatus);
    } 
}
 
 
@end