zoukankan      html  css  js  c++  java
  • 获得网络状态和实时监控网络状态改变

     

    本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/7031343

     

    目录(?)[+]

     

    Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。

    Reachability 中定义了3种网络状态:

    • NotReachable

      无连接

    • ReachableViaCarrierDataNetwork (ReachableViaWWAN)

      使用3G/GPRS网络

    • ReachableViaWiFiNetwork (ReachableViaWiFi)

      使用WiFi网络

    检测莫个特定站点的连接状况

    1. Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];  
    2. switch ([r currentReachabilityStatus])  
    3. {  
    4.     case NotReachable:  
    5.         // 没有网络连接  
    6.         break;  
    7.     case ReachableViaWWAN:  
    8.         // 使用3G网络  
    9.         break;  
    10.     case ReachableViaWiFi:  
    11.         // 使用WiFi网络  
    12.         break;  
    13. }  

    检测当前网络环境

    1. // 是否wifi  
    2. + (BOOL) IsEnableWIFI   
    3. {  
    4.     return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);  
    5. }  
    6.   
    7. // 是否3G  
    8. + (BOOL) IsEnable3G   
    9. {  
    10.     return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);  
    11. }  

    连接状态实时通知

    网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户。由于Reachability1.5版与2.0版有一些变化,这里分开来说明使用方法。

    Reachability 1.5

    1. // My.AppDelegate.h  
    2.   
    3. #import "Reachability.h"  
    4.   
    5. @interface MyAppDelegate : NSObject <UIApplicationDelegate> {  
    6.     NetworkStatus remoteHostStatus;  
    7. }  
    8.   
    9. @property NetworkStatus remoteHostStatus;  
    10.   
    11. @end  
    12.   
    13. // My.AppDelegate.m  
    14.   
    15. #import "MyAppDelegate.h"  
    16.   
    17. @implementation MyAppDelegate  
    18.   
    19. @synthesize remoteHostStatus;  
    20.   
    21. // 更新网络状态  
    22. - (void)updateStatus {  
    23.     self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];  
    24. }  
    25.   
    26. // 通知网络状态  
    27. - (void)reachabilityChanged:(NSNotification *)note {  
    28.     [self updateStatus];  
    29.     if (self.remoteHostStatus == NotReachable) {  
    30.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil) message:NSLocalizedString(@"NotReachable", nil)  
    31.         delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];  
    32.         [alert show];  
    33.         [alert release];  
    34.     }  
    35. }  
    36.   
    37. // 程序启动器,启动网络监视  
    38. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
    39.   
    40.     // 设置网络检测的站点  
    41.     [[Reachability sharedReachability] setHostName:@"www.apple.com"];  
    42.     [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];  
    43.     // 设置网络状态变化时的通知函数  
    44.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)  
    45.                                                  name:@"kNetworkReachabilityChangedNotification" object:nil];  
    46.     [self updateStatus];  
    47. }  
    48.   
    49. - (void)dealloc {  
    50.     // 删除通知对象  
    51.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
    52.     [window release];  
    53.     [super dealloc];  
    54. }  


    Reachability 2.0

    1. // MyAppDelegate.h  
    2.   
    3. @class Reachability;  
    4.   
    5. @interface MyAppDelegate : NSObject <UIApplicationDelegate> {  
    6.     Reachability  *hostReach;  
    7. }  
    8.   
    9. @end  
    10.   
    11. // MyAppDelegate.m  
    12. - (void)reachabilityChanged:(NSNotification *)note {  
    13.     Reachability* curReach = [note object];  
    14.     NSParameterAssert([curReach isKindOfClass: [Reachability class]]);  
    15.     NetworkStatus status = [curReach currentReachabilityStatus];  
    16.   
    17.     if (status == NotReachable) {  
    18.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""  
    19.                                                         message:@"NotReachable"  
    20.                                                        delegate:nil  
    21.                                               cancelButtonTitle:@"YES" otherButtonTitles:nil];  
    22.         [alert show];  
    23.         [alert release];  
    24.     }  
    25. }  
    26.   
    27. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
    28.     // ...  
    29.   
    30.     // 监测网络情况  
    31.     [[NSNotificationCenter defaultCenter] addObserver:self  
    32.                                           selector:@selector(reachabilityChanged:)  
    33.                                           name: kReachabilityChangedNotification  
    34.                                           object: nil];  
    35.     hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];  
    36.     [hostReach startNotifer];  
    37.     // ...  
    38. }  
  • 相关阅读:
    Thinkphp下实现D函数用于实例化Model格式
    Thinkphp3.2下导入所需的类库 同java的Import 本函数有缓存功能
    Thinkphp下记录和统计时间(微秒)和内存使用情况
    python打造seo必备工具-自动查询排名
    Python爬虫爬企查查数据
    解决Android8.0系统应用打开webView报错
    团队冲刺第七天个人博客
    团队冲刺第六天个人博客
    团队冲刺第五天个人博客
    团队冲刺第四天个人博客
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3601818.html
Copyright © 2011-2022 走看看