iOS監測網絡狀態

1.通過AFNetworking的AFNetworkReachabilityManager對象

監聽網絡方法

- (void)netWorkMonitor{
    manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                [ProgressHUD showSuccess:@"未識別的網絡" Interaction:YES];
                break;
            case AFNetworkReachabilityStatusNotReachable:
                [ProgressHUD showSuccess:@"網絡不可用,請打開WiFi或者移動蜂窩網絡" Interaction:YES];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                [ProgressHUD showError:@"2G,3G,4G...的網絡" Interaction:YES];
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                [ProgressHUD showSuccess:@"wifi的網絡" Interaction:YES];
                break;
            default:
                break;
        }
    }];
    
    [manager startMonitoring];
}

調用

  [self performSelector:@selector(netWorkMonitor) withObject:nil afterDelay:0.2];
2.Reachability對象
  • 必須將 Reachability.h 和 Reachability.m 拷貝到工程中。
  • 導入#import "Reachability.h"
//監聽部分
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
// 讓Reachability對象開啟被監聽狀態
[self.reach startNotifier];

//網絡狀態改變,彈出alertview提示框。
- (void)reachabilityChanged:(NSNotification *)note
{
    // 通過通知對象獲取被監聽的Reachability對象
    Reachability *curReach = [note object];
    
    // 獲取Reachability對象的網絡狀態
    NetworkStatus status = [curReach currentReachabilityStatus];
    
    if (status == NotReachable)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"不能訪問網絡,請檢查網絡" delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil];
        [alert show];
    }
}

注意:使用Reachability發通知的時候,在模擬器中網絡狀態改變reachabilityChanged一直會被調用兩次,查了好久不知道什么原因。這種情況不知道只有我碰到了還是?

  • Reachability寫法2
-(void)checkInternet
{
    self.internetReachability = [Reachability reachabilityForInternetConnection];
    if (self.internetReachability.currentReachabilityStatus==NotReachable)
    {
        NSLog(@"網絡不可用...");
        [self internetAlertView];
    } else{
        NSLog(@"網絡可用...");
    }
    [self.internetReachability startNotifier];
}

-(void)internetAlertView
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不能訪問網絡,請檢查網絡!" delegate:self cancelButtonTitle:@"OK"  otherButtonTitles:nil];
    [alertView show];
}

Reachability的使用,蘋果官方demo地址

3.使用netdb.h
  • 必須導入如下頭文件

include<unistd.h>

include<netdb.h>

+ 在Appdelegate.h文件中

-(BOOL)checkInternetConnection;


+ 在Appdelegate.m文件中 

-(BOOL)checkInternetConnection {

char *hostname;
struct hostent *hostinfo;
hostname = "baidu.com";
hostinfo = gethostbyname (hostname);
if (hostinfo == NULL)
{
    NSLog(@"-> no connection!\n");
    return NO;
}
else{
    NSLog(@"-> connection established!\n");
    return YES;
}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容