zoukankan      html  css  js  c++  java
  • iOS 判断当前网络状态

    1.如果只判断当前是否是无网的状态:

    if([Reachability reachabilityForLocalWiFi].currentReachabilityStatus==NotReachable&&[[Reachability reachabilityForInternetConnection] currentReachabilityStatus]==NotReachable)

    {

      //没网的操作

    }else

    {

      //有网的操作

    }

    2.监听网络状态的改变

    导入头文件 

    #import "Reachability.h"

    //注册通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange:) name:kReachabilityChangedNotification object:nil];

        self.conn = [Reachability reachabilityForInternetConnection];

        [self.conn startNotifier];

    (以上代码的conn是Reachability对象)

    - (void)networkStateChange:(NSNotification *)note

    {

        // 通过通知对象获取被监听的Reachability对象

        Reachability *curReach = [note object];

        // 获取Reachability对象的网络状态

        NetworkStatus status = [curReach currentReachabilityStatus];

        if (status == ReachableViaWWAN)

        {

            NSLog(@"3G在线");

        }else if (status == ReachableViaWiFi)

        {

            NSLog(@"WIFI在线");

        }else if (status == NotReachable)

        {

            NSLog(@"没网");

        }

    }

  • 相关阅读:
    递推数列
    大数阶乘
    成绩排序
    DevC++ return 1 exit status
    POJ 1061 青蛙的约会
    ZOJ 2750 Idiomatic Phrases Game
    nyoj 545 Metric Matrice
    nyoj 308 Substring
    nyoj 515完全覆盖 II
    nyoj 1248 海岛争霸
  • 原文地址:https://www.cnblogs.com/cui-cui/p/6378490.html
Copyright © 2011-2022 走看看