項(xiàng)目中常常需要監(jiān)聽(tīng)程序的網(wǎng)絡(luò)變化搔驼,判斷用戶(hù)是以哪種上網(wǎng)方式囊榜,在網(wǎng)上搜索了一番舅柜,找到了兩個(gè)常用的監(jiān)聽(tīng)網(wǎng)絡(luò)的方法瞒窒,第一種是使用AFNetworking中的AFNetworkReachabilityManager,第二種是使用蘋(píng)果推薦的Reachability恋技,如果你以為我這里在寫(xiě)下它倆的用法拇舀,那你就錯(cuò)了(好吧,是我錯(cuò)了蜻底,還是要寫(xiě)下這兩個(gè)的用法)骄崩。請(qǐng)看標(biāo)題,為什么叫真·實(shí)時(shí)監(jiān)聽(tīng)網(wǎng)絡(luò)變化,這兩種方法確實(shí)能監(jiān)聽(tīng)到網(wǎng)絡(luò)的狀態(tài)刁赖,但是有一種情況搁痛,就是鏈接上了WiFi长搀,但是這個(gè)WiFi是沒(méi)有網(wǎng)的宇弛,此時(shí)用那兩種方法只能判斷當(dāng)前設(shè)備鏈接的WiFi,無(wú)法判斷能不能連接到互聯(lián)網(wǎng)源请。努力上午搜尋了一下枪芒,發(fā)現(xiàn)一個(gè)開(kāi)源的三方挺好用,這里分享下(先貼下AFNetworkReachabilityManager和Reachability谁尸,有些情況下還是需要它倆的舅踪,做個(gè)記錄)。
以下都是在AppDelegate文件中編寫(xiě)的良蛮,有添加通知的是實(shí)時(shí)監(jiān)聽(tīng)網(wǎng)絡(luò)抽碌,未添加通知的地方如需要實(shí)時(shí)監(jiān)聽(tīng)請(qǐng)自行添加,添加完別忘移除通知(有點(diǎn)啰嗦决瞳,誰(shuí)讓我記性差)货徙。
AFNetworkReachabilityManager
-下載AFNetworking
- 包含頭文件#import "AFNetworking.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self listenNetWorkingStatus]; //監(jiān)聽(tīng)網(wǎng)絡(luò)是否可用
}
-(void)listenNetWorkingStatus{
//1:創(chuàng)建網(wǎng)絡(luò)監(jiān)聽(tīng)者
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager manager];
//2:獲取網(wǎng)絡(luò)狀態(tài)
/*
AFNetworkReachabilityStatusUnknown = 未知網(wǎng)絡(luò),
AFNetworkReachabilityStatusNotReachable = 沒(méi)有聯(lián)網(wǎng)
AFNetworkReachabilityStatusReachableViaWWAN = 蜂窩數(shù)據(jù)
AFNetworkReachabilityStatusReachableViaWiFi = 無(wú)線網(wǎng)
*/
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知網(wǎng)絡(luò)");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"沒(méi)有聯(lián)網(wǎng)");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"蜂窩數(shù)據(jù)");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"無(wú)線網(wǎng)");
break;
default:
break;
}
}];
//開(kāi)啟網(wǎng)絡(luò)監(jiān)聽(tīng)
[manager startMonitoring];
}
Reachability
- 下載Reachability皮胡,把Reachability.h Reachability.m文件導(dǎo)入工程
- 包含頭文件#import "Reachability.h"
- 生成兩個(gè)全局對(duì)象
@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self listenNetWorkingStatus]; //監(jiān)聽(tīng)網(wǎng)絡(luò)是否可用
}
-(void)listenNetWorkingStatus{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
// 設(shè)置網(wǎng)絡(luò)檢測(cè)的站點(diǎn)
NSString *remoteHostName = @"www.apple.com";
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
[self.hostReachability startNotifier];
[self updateInterfaceWithReachability:self.hostReachability];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
}
- (void) reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
[self updateInterfaceWithReachability:curReach];
}
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
NetworkStatus netStatus = [reachability currentReachabilityStatus];
switch (netStatus) {
case 0:
NSLog(@"NotReachable----無(wú)網(wǎng)絡(luò)");
break;
case 1:
NSLog(@"ReachableViaWiFi----WIFI");
break;
case 2:
NSLog(@"ReachableViaWWAN----蜂窩網(wǎng)絡(luò)");
break;
default:
break;
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
真·實(shí)時(shí)監(jiān)聽(tīng)網(wǎng)絡(luò)變化 RealReachability
- 點(diǎn)上面的名稱(chēng)下載
- 把RealReachability文件夾拷貝到項(xiàng)目中
- 包含頭文件#import "RealReachability.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self listenNetWorkingStatus]; //監(jiān)聽(tīng)網(wǎng)絡(luò)是否可用
}
-(void)listenNetWorkingStatus{
[GLobalRealReachability startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkChanged:)
name:kRealReachabilityChangedNotification
object:nil];
ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
[self realNetworkingStatus:status];
}
- (void)networkChanged:(NSNotification *)notification
{
RealReachability *reachability = (RealReachability *)notification.object;
ReachabilityStatus status = [reachability currentReachabilityStatus];
[self realNetworkingStatus:status];
}
-(void)realNetworkingStatus:(ReachabilityStatus)status{
switch (status)
{
case RealStatusUnknown:
{
NSLog(@"~~~~~~~~~~~~~RealStatusUnknown");
[self showNetworkStatusAlert:@"當(dāng)前網(wǎng)絡(luò)不可用"];
break;
}
case RealStatusNotReachable:
{
NSLog(@"~~~~~~~~~~~~~RealStatusNotReachable");
[self showNetworkStatusAlert:@"無(wú)網(wǎng)絡(luò),請(qǐng)檢查網(wǎng)絡(luò)鏈接"];
break;
}
case RealStatusViaWWAN:
{
NSLog(@"~~~~~~~~~~~~~RealStatusViaWWAN");
[self showNetworkStatusAlert:@"流量上網(wǎng)"];
break;
}
case RealStatusViaWiFi:
{
NSLog(@"~~~~~~~~~~~~~RealStatusViaWiFi");
// [self showNetworkStatusAlert:@"WIFI上網(wǎng),盡情揮霍吧小寶貝~"];
break;
}
default:
break;
}
}
-(void)showNetworkStatusAlert:(NSString *)str{
//我這里是網(wǎng)絡(luò)變化彈出一個(gè)警報(bào)框痴颊,由于不知道怎么讓widow加載UIAlertController,所以這里用UIAlertView替代了
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
幫助到你的話請(qǐng)點(diǎn)喜歡并來(lái)波關(guān)注屡贺,點(diǎn)關(guān)注蠢棱,不迷路(個(gè)人原創(chuàng))