1. 程式人生 > >iOS-網絡檢測的幾種方法

iOS-網絡檢測的幾種方法

else required 判斷網絡狀態 sha geb all ane mes nsinteger

1.AFN框架中的:AFNetworkReachabilityManager

//AFN判斷網絡

-(void)getInternetStatue{

// 1.獲得網絡監控的管理者

AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManagersharedManager];

// 2.設置網絡狀態改變後的處理

[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatusstatus) {

// 當網絡狀態改變了,就會調用這個block

switch (status) {

caseAFNetworkReachabilityStatusUnknown://未知網絡

NSLog(@"未知網絡");

break;

caseAFNetworkReachabilityStatusNotReachable://沒有網絡(斷網)

NSLog(@"沒有網絡(斷網)");

break;

caseAFNetworkReachabilityStatusReachableViaWWAN://手機自帶網絡

NSLog(@"手機自帶網絡");

break;

caseAFNetworkReachabilityStatusReachableViaWiFi:// WIFI

NSLog(@"WIFI");

break;

}

if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status ==AFNetworkReachabilityStatusReachableViaWiFi)

{

self.wifiStatue=@"有網絡";

NSLog(@"有網");

}else

{

self.wifiStatue=@"沒網絡";

   NSLog(@"沒有網");

UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"網絡失去連接"message:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:nil,nil];

alert.delegate =self;

[alert show];

}

}];

// 3.開始監控

[mgr startMonitoring];

}

========= 2.蘋果自帶reachability =========

1、代表網絡狀態的枚舉:

typedef enum : NSInteger {
    NotReachable = 0, //無網絡可用
    ReachableViaWiFi, //WiFi連接
    ReachableViaWWAN //無限廣域網連接
} NetworkStatus;
2、下面是相關接口和註釋
/*!
 * 用於檢測網絡請求是否可以到達指定的主機名
 */
+ (instancetype)reachabilityWithHostName:(NSString *)hostName;

/*!
 * 用於檢測網絡請求是否可以到達給定的ip地址
 */
+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;

/*!
 * 檢查默認的路由器是否有效. 用於不連接到特定主機的應用.
 */
+ (instancetype)reachabilityForInternetConnection;

/*!
 * 檢測本地的WiFi連接是否有效
 */
+ (instancetype)reachabilityForLocalWiFi;

/*!
 * 開始監聽在當前的runloop中的通知.
 */
- (BOOL)startNotifier;
- (void)stopNotifier;

//獲取網絡狀態
- (NetworkStatus)currentReachabilityStatus;

/*!
 * 連接需求
 */
- (BOOL)connectionRequired;

3、網絡連接狀態改變時的通知標識
NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotification";
//需要把該工程中的Reachability.h 和 Reachability.m 拷貝到你的工程中,同時需要把SystemConfiguration.framework 添加到工程中,

//創建Reachability

Reachability *reach = [Reachability reachabilityForInternetConnection];

reach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;

// 開始監控網絡(一旦網絡狀態發生改變, 就會發出通知kReachabilityChangedNotification)

[reach startNotifier];

//開啟通知---方式一:

// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange:) name:kReachabilityChangedNotification object:nil];

//// 處理網絡狀態改變

- (void)networkStateChange:(NSNotification *)note{

Reachability * reach = [noti object];

NetworkStatus status = [reach currentReachabilityStatus];

// 3.判斷網絡狀態

if (status== NotReachable) {

NSLog(@"沒網");

} else if (status==ReachableViaWWAN) {

NSLog(@"使用手機自帶網絡進行上網");

} else if(status==ReachableViaWiFi){

NSLog(@"通過wifi");

}}

註冊通知----方式二:

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(reachabilityChanged:)

name:kReachabilityChangedNotification

object:nil];//網絡狀態監控

- (void)reachabilityChanged:(NSNotification *)noti {

Reachability * reach = [noti object];

if([reach isReachable]) {

[self setMBProgressWithNumTip:@"網絡已連接"];

NSLog(@"網絡已經連接");

[LYCommanManager sharedManager].isreachable=[reachisReachable];

[[NSNotificationCenterdefaultCenter]postNotificationName:@"startloading" object:nil];//發送通知開始加載網頁

} else {

NSLog(@"網絡未連接");

[self setMBProgressWithNumTip:@"網絡未連接"];

[LYCommanManager sharedManager].isreachable=[reachisReachable];

[[NSNotificationCenterdefaultCenter]postNotificationName:@"endloading" object:nil];//發送通知結束加載

}

}

-(void)viewdidoad{

if([LYCommanManager sharedManager].isreachable){

[self setMBProgressWithNum:@"loading..."];

NSLog(@"當前是有網de");

}else {

[self setMBProgressWithNumTip:@"無網絡連接"];

NSLog(@"當前沒有網絡");

}

[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(finishLoading) name:@"endloading" object:nil];

[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(startLoading) name:@"startloading"object:nil];

}

//聯網後重新加載網頁

-(void)startLoading{

[self setTrainTicketWithMKweb];

}

//結束加載提示

-(void)finishLoading{

[MBProgressHUD hideHUDForView:self.view animated:YES];

}

=========3.通過狀態欄判斷網絡=========

//從狀態欄中獲取網絡類型,代碼如下:

-(NSString *)getNetWorkStates{

UIApplication *app = [UIApplicationsharedApplication];

NSArray *children = [[[appvalueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];

NSLog(@"---%@---",children);

NSString *state = [[NSStringalloc]init];

NSLog(@"--空字符串0-%@",state);

int netType =0;

//獲取到網絡返回碼

for (id childin children) {

if([childisKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {

//獲取到狀態欄

netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];

NSLog(@"netType---%d",netType);

switch (netType) {

case 0:

state = @"無網絡";

//無網模式

break;

case 1:

state = @"2G";

break;

case 2:

state = @"3G";

break;

case 3:

state = @"4G";

break;

case 5:

{

state = @"wifi";

NSLog(@"5");

break;

default:

break;

}

}

}

//根據狀態選擇

}

return state;

}

iOS-網絡檢測的幾種方法