zoukankan      html  css  js  c++  java
  • ios之网络监听

    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2. {  
    3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
    4.       
    5.     //开启网络状况的监听   
    6.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];  
    7.       
    8.     self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;  
    9.     [self.hostReach startNotifier];  //开始监听,会启动一个run loop   
    10.   
    11.     self.window.rootViewController = self.tabBarController;  
    12.     [self.window makeKeyAndVisible];  
    13.     return YES;  
    14. }  
    15.   
    16. //网络链接改变时会调用的方法   
    17. -(void)reachabilityChanged:(NSNotification *)note  
    18. {  
    19.     Reachability *currReach = [note object];  
    20.     NSParameterAssert([currReach isKindOfClass:[Reachability class]]);  
    21.       
    22.     //对连接改变做出响应处理动作   
    23.     NetworkStatus status = [currReach currentReachabilityStatus];  
    24.     //如果没有连接到网络就弹出提醒实况   
    25.     self.isReachable = YES;  
    26.     if(status == NotReachable)  
    27.     {  
    28.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"暂无法访问书城信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];  
    29.         [alert show];  
    30.         [alert release];  
    31.         self.isReachable = NO;  
    32.     }  
    33.     else  
    34.     {  
    35.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];  
    36.         [alert show];  
    37.         [alert release];  
    38.         self.isReachable = YES;  
    39.     }  
    40. }  
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        
        //开启网络状况的监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        
        self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
        [self.hostReach startNotifier];  //开始监听,会启动一个run loop
    
        self.window.rootViewController = self.tabBarController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    //网络链接改变时会调用的方法
    -(void)reachabilityChanged:(NSNotification *)note
    {
        Reachability *currReach = [note object];
        NSParameterAssert([currReach isKindOfClass:[Reachability class]]);
        
        //对连接改变做出响应处理动作
        NetworkStatus status = [currReach currentReachabilityStatus];
        //如果没有连接到网络就弹出提醒实况
        self.isReachable = YES;
        if(status == NotReachable)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"暂无法访问书城信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
            self.isReachable = NO;
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
            self.isReachable = YES;
        }
    }
    


    通过如上代码,在应用程序的任何一个界面都可以使用下面的单例来判断网络是否连接

    1. AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];  
    2. if(appDlg.isReachable)  
    3. {  
    4.     NSLog(@"网络已连接");//执行网络正常时的代码   
    5. }  
    6. else  
    7. {  
    8.     NSLog(@"网络连接异常");//执行网络异常时的代码   
    9. }  
  • 相关阅读:
    windows service 安装和卸载指令
    jackson 进行json与java对象转换 之四
    jackson 进行json与java对象转换 之三
    jackson 进行json与java对象转换 之二
    jackson 进行json与java对象转换 之一
    jackson2.x与Jackson1.9的比较
    判断Integer值相等不能用==
    java:String使用equals和==比较的区别
    Java中自定义枚举(Enum)项的值,可设置为指定的值
    Java enum(枚举)使用详解之四
  • 原文地址:https://www.cnblogs.com/lyanet/p/3031138.html
Copyright © 2011-2022 走看看