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

    使用之前请从Apple网站下载示例:点此下载

    然后将Reachability.h 和 Reachability.m 加到自己的项目中,并引用 SystemConfiguration.framework,就可以使用了。

    效果1:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        //检测网络情况
        [self startNotificationNetwork];
      
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    - (void)reachabilityChanged:(NSNotification *)notification{
        Reachability *currentReach = [notification object];
        NSParameterAssert([currentReach isKindOfClass:[Reachability class]]);
        NetworkStatus status = [currentReach currentReachabilityStatus];
        NSString *netMsg = nil;
        switch (status) {
            case NotReachable:
            {
                netMsg = @"网络不可用";
                break;
            }
            case ReachableViaWiFi:
            {
                netMsg = @"通过WiFi上网";
                break;
            }
            case ReachableViaWWAN:
            {
                netMsg = @"通过3G/GPRS上网";
                break;
            }
        }
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"联网提示" message:netMsg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    
    -(void)startNotificationNetwork{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        Reachability * hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];
        [hostReach startNotifier];
    }

    致谢:http://www.cnblogs.com/mrhgw/archive/2012/08/01/2617760.html

     效果2:

    //处理连接改变后的情况 //对连接改变做出响应的处理动作。
    - (void)updateInterfaceWithReachability: (Reachability*) curReach
    {
        NetworkStatus status = [curReach currentReachabilityStatus];
        
        if(status ==NotReachable) {
            UIAlertView*alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示"
                                                              message:@"网络连接失败,请检查网络"
                                                             delegate:nil
                                                    cancelButtonTitle:@"确定"
                                                    otherButtonTitles:nil];
            [alertView show];
            [alertView release];
        }else{
            NSLog(@"connect with the internet successfully");
        }
        
    }
    
    
     //连接改变
    - (void)reachabilityChanged:(NSNotification* )note
    {
        Reachability* curReach = [note object];
        NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
        [self updateInterfaceWithReachability: curReach];
    }
    
    
    -(void)startNotificationNetwork{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        Reachability * hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];
        [hostReach startNotifier];:
    }

    致谢:http://blog.sina.com.cn/s/blog_91ff71c001016gql.html

  • 相关阅读:
    TensorFlow进阶(六)---模型保存与恢复、自定义命令行参数
    TensorFlow进阶(五)---图与会话
    TensorFlow进阶(四)---名称域和共享变量
    spark中数据倾斜解决方案
    Hive窗口函数之LAG、LEAD、FIRST_VALUE、LAST_VALUE的用法
    java.lang.RuntimeException: HRegionServer Aborted
    hive中的优化问题
    读取hbase数据到mysql
    用mapreduce读取hdfs数据到hbase上
    centos7下安装elasticSearch错误总结(单节点模式)
  • 原文地址:https://www.cnblogs.com/hanjun/p/3044283.html
Copyright © 2011-2022 走看看