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. }  
  • 相关阅读:
    vue中的 computed 和 watch 的区别
    mysql8.0 初始化数据库及表名大小写问题
    sql server alwayson 调整数据文件路径
    zabbix 自定义监控 SQL Server
    mysql 创建用户及授权
    mysql 设置从库只读模式
    mysql8.0 主从复制安装及配置
    centos8.0安装mysql8.0
    centos8替换阿里数据源
    npm publish 报错 【you or one of your dependencies are requesting a package version that is forbidden by your security policy】
  • 原文地址:https://www.cnblogs.com/lyanet/p/3031138.html
Copyright © 2011-2022 走看看