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(@"没网");

        }

    }

  • 相关阅读:
    构建之法第九、十章读后感
    构建之法第七章读后感
    构建之法五、六章读后感
    构建之法第四章读后感
    一组阶段小记之再读构建之法
    打印控件
    TTS语音
    VS2005通过网络连接CE设备进行调试开发
    Windows XP下安装WinCE6.0开发环境
    利用VS2005创建WINCE 6.0 平台
  • 原文地址:https://www.cnblogs.com/cui-cui/p/6378490.html
Copyright © 2011-2022 走看看