vc中使用 viewDidLoad---dealloc willAppear --- willDidDisappear
#pragma mark - 蘋果提供的方法
/// 當檢測到網(wǎng)絡斷開時會間隔15s再次檢測例隆,如果還是斷開則彈窗提醒核畴,防止過于頻繁操作
- (void)appleReachabilityTest {
/// Reachability使用了通知,當網(wǎng)絡狀態(tài)發(fā)生變化時發(fā)送通知kReachabilityChangedNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appReachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
// 被通知函數(shù)運行的線程應該由startNotifier函數(shù)執(zhí)行的線程決定
typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString *remoteHostName = @"www.bing.com";
/**
* 有可能有問題這個帶host的實例化方法--判斷不準確--可以用reachabilityForInternetConnection來替代
*/
weakSelf.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
[weakSelf.hostReachability startNotifier];
weakSelf.routerReachability = [Reachability reachabilityForInternetConnection];
[weakSelf.routerReachability startNotifier];
// 開啟當前線程消息循環(huán)
[[NSRunLoop currentRunLoop] run];
});
}
/// 當網(wǎng)絡狀態(tài)發(fā)生變化時調用
- (void)appReachabilityChanged:(NSNotification *)notification{
Reachability *reach = [notification object];
if([reach isKindOfClass:[Reachability class]]){
NetworkStatus status = [reach currentReachabilityStatus];
// 兩種檢測:路由與服務器是否可達 三種狀態(tài):手機流量聯(lián)網(wǎng)柿菩、WiFi聯(lián)網(wǎng)、沒有聯(lián)網(wǎng)
if (reach == self.routerReachability) {
if (status == NotReachable) {
// NSLog(@"routerReachability NotReachable");
// 15秒后再次檢測
[self performSelector:@selector(appReachabilityChangedConfirm) withObject:nil afterDelay:15];
} else if (status == ReachableViaWiFi) {
NSLog(@"routerReachability ReachableViaWiFi--%@",[NSThread currentThread]);
} else if (status == ReachableViaWWAN) {
NSLog(@"routerReachability ReachableViaWWAN");
} else if (status == kReachableVia4G){
NSLog(@"routerReachability ReachableVia4G");
} else if(status == kReachableVia3G){
NSLog(@"routerReachability ReachableVia3G");
}else if(status == kReachableVia2G){
NSLog(@"routerReachability ReachableVia2G");
}
}
if (reach == self.hostReachability) {
if (status == NotReachable) {
NSLog(@"hostReachability failed");
}else if (status == ReachableViaWiFi) {
NSLog(@"hostReachability ReachableViaWiFi--%@",[NSThread currentThread]);
} else if (status == ReachableViaWWAN) {
NSLog(@"hostReachability ReachableViaWWAN");
} else if (status == kReachableVia4G){
NSLog(@"hostReachability ReachableVia4G");
} else if(status == kReachableVia3G){
NSLog(@"hostReachability ReachableVia3G");
}else if(status == kReachableVia2G){
NSLog(@"hostReachability ReachableVia2G");
}
}
}
}
/// 再次檢測網(wǎng)絡棠众,如果還是斷開予借,則視為網(wǎng)絡已經(jīng)斷開
- (void)appReachabilityChangedConfirm {
if([self.routerReachability isKindOfClass:[Reachability class]]){
if ([self.routerReachability currentReachabilityStatus] == NotReachable) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"網(wǎng)絡中斷" message:@"請檢查網(wǎng)絡" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okBtn = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:cancelBtn];
[alert addAction:okBtn];
[self presentViewController:alert animated:YES completion:nil];
});
}
}
}
修改源文件增加檢測2G/3G/4G功能
.h修改枚舉值
typedef enum : NSInteger { NotReachable = 0, ReachableViaWiFi, ReachableViaWWAN, kReachableVia4G, kReachableVia2G, kReachableVia3G } NetworkStatus;
.m
增加頭文件
#import <UIKit/UIDevice.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
/Radio Access Technology values/
然后修改此方法
- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
{
PrintReachabilityFlags(flags, "networkStatusForFlags");
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
{
// The target host is not reachable.
return NotReachable;
}
NetworkStatus returnValue = NotReachable;
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
{
/*
If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi...
*/
returnValue = ReachableViaWiFi;
}
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
{
/*
... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
*/
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
{
/*
... and no [user] intervention is needed...
*/
returnValue = ReachableViaWiFi;
}
}
//
// if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
// {
// /*
// ... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
// */
// returnValue = ReachableViaWWAN;
// }
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
{
/*
... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
*/
returnValue = ReachableViaWWAN;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
CTTelephonyNetworkInfo *phonyNetwork = [[CTTelephonyNetworkInfo alloc] init];
NSString *currentStr = phonyNetwork.currentRadioAccessTechnology;
if (currentStr) {
if ([currentStr isEqualToString:CTRadioAccessTechnologyLTE]) {
returnValue = kReachableVia4G;
}else if ([currentStr isEqualToString:CTRadioAccessTechnologyGPRS]|| [currentStr isEqualToString:CTRadioAccessTechnologyEdge]){
returnValue = kReachableVia2G;
}else{
returnValue = kReachableVia3G;
}
}
}else{
if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection) {
returnValue = kReachableVia3G;
if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired) {
returnValue = kReachableVia2G;
}
}
}
}
return returnValue;
}
<發(fā)現(xiàn)有時候通知會發(fā)出2次;有時候通知發(fā)出1次?why!>
我覺得應該發(fā)出2次才對啊 startNotifier方法調用了2次啊!
demo地址:
https://github.com/jiangweike/networkStatus
參考文章
https://blog.6ag.cn/1310.html
http://xiongzenghuidegithub.github.io/blog/2013/12/30/reachabilityyuan-ma-xue-xi/
http://blog.csdn.net/Axing1991/article/details/46649939
http://www.reibang.com/p/d95c8879d8d5
http://www.cnblogs.com/mddblog/p/5304346.html
http://www.cnblogs.com/scut-linmaojiang/p/iOS-Reachability.html
https://github.com/mddios/NetworkStauts
http://blog.csdn.net/applelg/article/details/47660577
http://www.ithao123.cn/content-2198058.html
https://www.mgenware.com/blog/?p=487
http://www.reibang.com/p/1b0901d0902b