直接上代码不废话
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
[GLobalRealReachability startNotifier];
return YES;
}
#import "NetworkTestLoading.h"
//检测网络状态
#import "RealReachability.h"
#import "NSObject+SimpleKVO.h"
#import "AFNetworkReachabilityManager.h"
@interface NetworkTestLoading ()
@end
@implementationNetworkTestLoading
+ (NetworkTestLoading*)sharedInstance
{
static NetworkTestLoading* share = nil;
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
share = [[superallocWithZone:NULL]init];
});
returnshare;
}
+ (instancetype)allocWithZone:(struct_NSZone*)zone {
return [self sharedInstance];
}
- (id)copyWithZone:(struct_NSZone*)zone {
return self;
}
- (id)mutableCopyWithZone:(struct_NSZone*)zone {
return self;
}
- (void)noNetworkTestLoading{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkChanged:)
name:kRealReachabilityChangedNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(VPNStatusChanged:)
name:kRRVPNStatusChangedNotification
object:nil];
ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
NSLog(@"Initial reachability status:%@",@(status));
[self setupFlagLabelWithStatus:status
isVPNOn:[GLobalRealReachabilityisVPNOn]
accessType:[GLobalRealReachabilitycurrentWWANtype]];
}
- (void)networkChanged:(NSNotification*)notification
{
RealReachability*reachability = (RealReachability*)notification.object;
ReachabilityStatusstatus = [reachabilitycurrentReachabilityStatus];
ReachabilityStatuspreviousStatus = [reachabilitypreviousReachabilityStatus];
NSLog(@"networkChanged, currentStatus:%@, previousStatus:%@", @(status), @(previousStatus));
[self setupFlagLabelWithStatus:status
isVPNOn:[GLobalRealReachabilityisVPNOn]
accessType:[GLobalRealReachabilitycurrentWWANtype]];
}
- (void)VPNStatusChanged:(NSNotification*)notification
{
// refreshing the status.
[self setupFlagLabelWithStatus:[GLobalRealReachability currentReachabilityStatus]
isVPNOn:[GLobalRealReachabilityisVPNOn]
accessType:[GLobalRealReachabilitycurrentWWANtype]];
}
- (void)setupFlagLabelWithStatus:(ReachabilityStatus)status
isVPNOn:(BOOL)isVPNOn
accessType:(WWANAccessType)accessType
{
NSMutableString*labelStr = [@""mutableCopy];
switch(status)
{
case RealStatusNotReachable:
{
[labelStrappendString:@"Network unreachable! 弱网"];
break;
}
case RealStatusViaWiFi:
{
[labelStrappendString:@"Network wifi! Free! "];
[[NSNotificationCenter defaultCenter] postNotificationName:@"noNetwork" object:@"YES"];
break;
}
case RealStatusViaWWAN:
{
[labelStrappendString:@"WWAN in charge! "];
break;
}
case RealStatusUnknown:
{
[labelStrappendString:@"Unknown status! Needs debugging! "];
break;
}
default:
{
[labelStrappendString:@"Status error! Needs debugging! "];
break;
}
}
if(isVPNOn)
{
[labelStrappendString:@"VPN On! "];
}
if (status == RealStatusViaWWAN)
{
NSString*descStr;
if(accessType ==WWANType2G)
{
descStr =@"2G";
}
elseif(accessType ==WWANType3G)
{
descStr =@"3G";
}
elseif(accessType ==WWANType4G)
{
descStr =@"4G";
}
else
{
descStr =@"Unknown Status, might be iOS6";
}
[labelStrappendString:descStr];
}
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch(status) {
case-1:
NSLog(@"未知网络");
break;
case0:
NSLog(@"网络不可达");
break;
case1:
{
NSLog(@"GPRS网络");
//发通知,带头搞事
}
break;
case2:
{
NSLog(@"wifi网络");
//发通知,搞事情
}
break;
default:
break;
}
if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
NSLog(@"有网");
}else{
[[NSNotificationCenter defaultCenter] postNotificationName:@"noNetwork" object:@"NO"];
NSLog(@"没网");
}
}];
}